Write a Python program that retrieves values a, b, and c from the user,
calculates and prints the roots of a quadratic equation for any given values of a, b and c.
import math
a = float(input("a = "))
b = float(input("b = "))
c = float(input("c = "))
discr = b ** 2 - 4 * a * c
if discr > 0:
x1 = (-b + math.sqrt(discr)) / (2 * a)
x2 = (-b - math.sqrt(discr)) / (2 * a)
print("x1 = %.2f \nx2 = %.2f" % (x1, x2))
elif discr == 0:
x = -b / (2 * a)
print("x = %.2f" % x)
else:
print("The equation has no roots")
Comments
Leave a comment