Untitled
unknown
python
6 months ago
1.1 kB
3
Indexable
import cmath def SqRoots(): print("Enter the coefficients a, b, and c of the quadratic equation (a*x^2 + b*x + c = 0) separated by spaces:") a, b, c = map(float, input().split()) # calculate the discriminant d = b**2 - 4*a*c # check if the discriminant is greater than zero if d > 0: # find two solutions sol1 = (-b-cmath.sqrt(d))/(2*a) sol2 = (-b+cmath.sqrt(d))/(2*a) # sort the solutions in ascending order if sol1.real < sol2.real: print("The roots of the equation are:", sol1.real, sol2.real) else: print("The roots of the equation are:", sol2.real, sol1.real) elif d == 0: # find one solution sol = -b / (2*a) print("The root of the equation is:", sol) else: # calculate the complex roots real = -b / (2*a) imag = cmath.sqrt(-d) / (2*a) # print the complex roots print("The roots of the equation are:", real, "+", imag, "i", "and", real, "-", imag, "i") # call the function SqRoots()
Editor is loading...
Leave a Comment