Python program to find the roots of quadratic Equation

Python program to find the roots of quadratic Equation and also find nature of roots (ax2+bx+c = 0)

In this article we will see Python program to find the roots of quadratic Equation and also find nature of roots (ax2+bx+c = 0).

Let’s look at the code now :-

#Program find Roots of Quadratic Equation and also find nature of roots
import math
# get the value of a,b,c from input() function
a = float(input("Enter the value of a "))
b = float(input("Enter the value of b "))
c = float(input("Enter the value of c "))

#calculate discriminant 
d = b*b-4*a*c

if d<0:
    print("Roots are imaginary, there are no Solution")
elif d==0:
     print("both roots are equal")
     x1=x2= -b/(2*a)
     print("Root1 = ",x1)
     print("Root2 = ",x2)
else:
     print("roots are real and different type")
     x1= (-b+math.sqrt(d))/(2*a)
     x2= (-b-math.sqrt(d))/(2*a)
     print("Root1 = ",x1)
     print("Root2 = ",x2)

OUTPUT :-

Enter the value of a 4
Enter the value of b 5
Enter the value of c 3
Roots are imaginary, there are no Solution
 
Enter the value of a 4
Enter the value of b 8
Enter the value of c 3
roots are real and different type
Root1 =  -0.5
Root2 =  -1.5
Enter the value of a 4
Enter the value of b 4
Enter the value of c 1
both roots are equal
Root1 =  -0.5
Root2 =  -0.5

Leave a Reply