Advertisement
yiwen_akeni

python - Quadratic equation

Jun 24th, 2020
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. print("programme to solves  Quadratic equation (equation of the second degree) ")
  2. print("this programme solves equations \n like  Ax²+Bx+C=0")
  3. while True:
  4.     num1 = input("Please enter the value of A : ")
  5.     try:
  6.         a = int(num1)
  7.         break
  8.     except ValueError:
  9.         try:
  10.             a = float(num1)
  11.             break
  12.         except ValueError:
  13.             print("This is not a number. Please enter a valid number")
  14. while True:
  15.     num2 = input("Please enter the value of B : ")
  16.     try:
  17.         b = int(num2)
  18.         break
  19.     except ValueError:
  20.         try:
  21.             b = float(num2)
  22.             break
  23.         except ValueError:
  24.             print("This is not a number. Please enter a valid number")
  25. while True:
  26.     num3 = input("Please enter the value of C : ")
  27.     try:
  28.         c = int(num3)
  29.         break
  30.     except ValueError:
  31.         try:
  32.             c = float(num3)
  33.             break
  34.         except ValueError:
  35.             print("This is not a number. Please enter a valid number")
  36. if a == 0 and b == 0:
  37.     print("the resulte is", c, "=0\n it's incorrect equation")
  38. elif a == 0 and b != 0:
  39.     x = c / b
  40.     print("the val of X is:\n x = ", x)
  41. elif a != 0 and b == 0:
  42.     from math import sqrt
  43.     from math import fabs
  44.  
  45.     x = fabs(c / a)
  46.     x = sqrt(x)
  47.     print("the val of X is:\n x1 = ", x, "    x2 = -", x)
  48. else:
  49.     from math import sqrt
  50.  
  51.     delta = (b ** 2) - (4 * a * c)
  52.     if delta < 0:
  53.         print("the equation: ", a, "x²+", b, "x+", c, "=0 has no real solution")
  54.     elif delta == 0:
  55.         x = b / (2 * a)
  56.         print("the equation: ", a, "x²+", b, "+", c, "=0 has unique solution")
  57.         print("x = ", x)
  58.     elif delta > 0:
  59.         from math import sqrt
  60.  
  61.         x1 = ((-b + sqrt(delta)) / (2 * a))
  62.         x2 = ((-b - sqrt(delta)) / (2 * a))
  63.         print("X has two value are :")
  64.         print("X = ", x1, "  or   X = ", x2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement