|
| 1 | +import math |
| 2 | +print("General form of quadratic is: ax^2 + bx + c, where (a!=0).") |
| 3 | +try: |
| 4 | + a = float(input("Enter the value for a: ")) |
| 5 | + if a == 0: |
| 6 | + print("a can't be equals to zero.") |
| 7 | + else: |
| 8 | + b = float(input("Enter the value for b: ")) |
| 9 | + c = float(input("Enter the value for c: ")) |
| 10 | + if c == 0: |
| 11 | + print(f"The quadratic is: {a}x² {'+' if b>0 else '-' } {abs(b)}x") |
| 12 | + elif b==0: |
| 13 | + print(f"The quadratic is: {a}x² {'+' if c>0 else '-' } {abs(c)}") |
| 14 | + else: |
| 15 | + print(f"The quadratic is: {a}x² {'+' if b>0 else '-' } {abs(b)}x {'+' if c>0 else '-' } {abs(c)}") |
| 16 | + |
| 17 | + d = b**2 - 4*a*c |
| 18 | + |
| 19 | + if d<0: |
| 20 | + print("This quadratic have complex roots.") |
| 21 | + if b==0: |
| 22 | + imag = round(((math.sqrt(-d))/(2*a)), 2) |
| 23 | + root1 = (f"i{imag}") |
| 24 | + root2 = (f"-i{imag}") |
| 25 | + print(f"The roots of given quadratic are \'{root1}\' and \'{root2}\'.") |
| 26 | + else: |
| 27 | + real = round((-b)/(2*a), 2) |
| 28 | + imag = round(((math.sqrt(-d))/(2*a)), 2) |
| 29 | + root1 = (f"{real} + i{imag}") |
| 30 | + root2 = (f"{real} - i{imag}") |
| 31 | + print(f"The roots of given quadratic are \'{root1}\' and \'{root2}\'.") |
| 32 | + elif d == 0: |
| 33 | + print("This quadratic have equal roots.") |
| 34 | + print(f"Which is \'{round((-b/(2*a)), 2)}\'.") |
| 35 | + |
| 36 | + else: |
| 37 | + print("This quadratic have two real and distinct roots.") |
| 38 | + root1 = round((-b + math.sqrt(d))/(2*a), 2) |
| 39 | + root2 = round((-b - math.sqrt(d))/(2*a), 2) |
| 40 | + print(f"Which are \'{root1}\' and \'{root2}\'.") |
| 41 | +except Exception as e: |
| 42 | + print(f"ERROR: {e}") |
0 commit comments