bUmmy1337 <3
unknown
python
a year ago
1.8 kB
4
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:
# round the solutions to two decimal places
sol1_real = round(sol1.real, 2)
sol1_imag = round(sol1.imag, 2)
sol2_real = round(sol2.real, 2)
sol2_imag = round(sol2.imag, 2)
print("The roots of the equation are:", sol1_real, sol1_imag, "i", "and", sol2_real, sol2_imag, "i")
else:
# round the solutions to two decimal places
sol1_real = round(sol1.real, 2)
sol1_imag = round(sol1.imag, 2)
sol2_real = round(sol2.real, 2)
sol2_imag = round(sol2.imag, 2)
print("The roots of the equation are:", sol2_real, sol2_imag, "i", "and", sol1_real, sol1_imag, "i")
elif d == 0:
# find one solution
sol = -b / (2*a)
# round the solution to two decimal places
sol = round(sol, 2)
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:", round(real, 2), "+", round(imag.real, 2), "i", "and", round(real, 2), "-", round(imag.real, 2), "i")
# call the function
SqRoots()
Editor is loading...
Leave a Comment