Write a program that computes the real roots of a given quadratic equation (Use math library).

Discriminant Δ = b 2 − 4 a c

Real Roots = −b ± √Δ/2 a

Code

import math

print("Enter the coefficients of the quadratic equation")
a = float(input("a: "))
b = float(input("b: "))
c = float(input("c: "))
dis_form = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis_form))

if a == 0:
print("The equation is not quadratic")
elif dis_form > 0:
print("The roots are real and different")
print((-b + sqrt_val) / (2 * a))
print((-b - sqrt_val) / (2 * a))
elif dis_form == 0:
print("The roots are real and same")
print(-b / (2 * a))
else:
print("The roots are complex")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)

Output