Skip to content
Home » Program to calculate and print roots of a quadratic equation: ax^2+bx+c=0

Program to calculate and print roots of a quadratic equation: ax^2+bx+c=0

Python Program to calculate roots of a quadratic equation: ax^2+bx+c=0

import math
print("For quadratic equation, ax**2+bx+c=0,enter coefficients below")
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=int(input("Enter c:"))
if a==0:
    print("Value of", a, "should not be zero")
else:
    delta=b*b-(4*a*c)
    if delta>0:
        root_1=(-b+math.sqrt(delta))/(2*a)
        root_2=(-b-math.sqrt(delta))/(2*a)
        print("Roots are real and unequal")
        print("Root_1=",root_1,"Root_2=",root_2)
    elif:
        root_1=-b/(2*a)
        print("Roots are real and equal")
        print("Root_1=",root_1,"Root_2=",root_2)
    else:
        print("Roots are complex and imaginary")

Output:

For quadratic equation, ax**2+bx+c=0,enter coefficients below

Enter a:2

Enter b:3

Enter c:1

Roots are real and unequal

Root_1= -0.5 Root_2= -1.0