|
| 1 | +from __future__ import print_function |
| 2 | +import math |
| 3 | + |
| 4 | +def QuadraticEquation(a,b,c): |
| 5 | + """ |
| 6 | + Prints the solutions for a quadratic equation, given the numerical coefficients a, b and c, |
| 7 | + for a*x*x + b*x + c. |
| 8 | + Ex.: a = 1, b = 3, c = -4 |
| 9 | + Solution1 = 1 and Solution2 = -4 |
| 10 | + """ |
| 11 | + Delta = b*b - 4*a*c |
| 12 | + if a != 0: |
| 13 | + if Delta >= 0: |
| 14 | + Solution1 = (-b + math.sqrt(Delta))/(2*a) |
| 15 | + Solution2 = (-b - math.sqrt(Delta))/(2*a) |
| 16 | + print ("The equation solutions are: ", Solution1," and ", Solution2) |
| 17 | + else: |
| 18 | + """ |
| 19 | + Treats cases of Complexes Solutions(i = imaginary unit) |
| 20 | + Ex.: a = 5, b = 2, c = 1 |
| 21 | + Solution1 = (- 2 + 4.0 *i)/2 and Solution2 = (- 2 + 4.0 *i)/ 10 |
| 22 | + """ |
| 23 | + if b > 0: |
| 24 | + print("The equation solutions are: (-",b,"+",math.sqrt(-Delta),"*i)/2 and (-",b,"+",math.sqrt(-Delta),"*i)/", 2*a) |
| 25 | + if b < 0: |
| 26 | + print("The equation solutions are: (",b,"+",math.sqrt(-Delta),"*i)/2 and (",b,"+",math.sqrt(-Delta),"*i/",2*a) |
| 27 | + if b == 0: |
| 28 | + print("The equation solutions are: (",math.sqrt(-Delta),"*i)/2 and ",math.sqrt(-Delta),"*i)/", 2*a) |
| 29 | + else: |
| 30 | + print("Error. Please, coeficient 'a' must not be zero for quadratic equations.") |
| 31 | +def main(): |
| 32 | + a = 5 |
| 33 | + b = 6 |
| 34 | + c = 1 |
| 35 | + |
| 36 | + QuadraticEquation(a,b,c) # The equation solutions are: -0.2 and -1.0 |
| 37 | + |
| 38 | + |
| 39 | +if __name__ == '__main__': |
| 40 | + main() |
0 commit comments