Python Program to Solve Quadratic Equation

We will develop a python program to solve the quadratic equation. It will find the roots of the given quadratic equation.

A quadratic equation is an equation of the second degree, meaning it contains at least one term that is squared. The standard form of the quadratic equation is ax² + bx + c = 0 where a, b and c are real and a !=0, x is an unknown variable. The nature of roots is determined by the discriminant.

Quadratic formula

The discriminant of the Quadratic equation is calculated as b²-4ac.

discriminant(d) = b² – 4*a*c

The nature of the roots are given as,

  • If discriminant>1 then the roots are real and different
  • If discriminant=0 then the roots are real and equal
  • If discriminant<1 then the roots are complex and different

For the quadratic equation ax² + bx + c = 0, if we denote the discriminant as d, then their roots

If d>1 then the roots are real and different
root1 = (-b + √d)/2a
root2 = (-b – √d)/2a

If d=0 then both roots are -b/2a

If d<1 then roots are complex and different
root1 = -b/2a + i (√d/2a)
root2 = -b/2a – i (√d/2a)

Write a Program to Solve Quadratic Equation using Python

This is a normal method to find roots of quadratic equation in python. We will take three numbers while declaring the variables. Python program to find roots of the quadratic equation using math module and if-else statement.

# Python program to find roots of quadratic equation

import math  #importing math-module

# take inputs
a = int(input('Enter the value of a: '))
b = int(input('Enter the value of b: '))
c = int(input('Enter the value of c: '))

# calculate discriminant
dis = (b**2) - (4*a*c)

# checking condition for discriminant
if(dis > 0):
    root1 = (-b + math.sqrt(dis) / (2 * a))
    root2 = (-b - math.sqrt(dis) / (2 * a))
    print("Two distinct real roots are %.2f and %.2f" %(root1, root2))

elif(dis == 0):
    root1 = root2 = -b / (2 * a)
    print("Two equal and real roots are %.2f and %.2f" %(root1, root2))

elif(dis < 0):
    root1 = root2 = -b / (2 * a)
    imaginary = math.sqrt(-dis) / (2 * a)
    print("Two distinct complex roots are %.2f+%.2f and %.2f-%.2f" 
                          %(root1, imaginary, root2, imaginary))

Output for the different input values:-

Enter the value of a: 5
Enter the value of b: 8
Enter the value of c: 3
Two distinct real roots are -7.80 and -8.20

Enter the value of a: 1
Enter the value of b: 2
Enter the value of c: 1
Two equal and real roots are -1.00 and -1.00

Enter the value of a: 2
Enter the value of b: 5
Enter the value of c: 4
Two distinct complex roots are -1.25+0.66 and -1.25-0.66

Note:- If you are given input a is 0, then the python program gets ZeroDivisionError

Find Roots of Quadratic Equation using Function

We can also take the help of a function to find the roots of the quadratic equation in python. A function is a block of code that performs a specific task.

# Python program to solve quadratic equation

import math  #importing math-module

def edu_roots(a, b, c):  #user-defined function
    # calculate discriminant
    dis = (b**2) - (4*a*c)

    # checking condition for discriminant
    if(dis > 0):
        root1 = (-b + math.sqrt(dis) / (2 * a))
        root2 = (-b - math.sqrt(dis) / (2 * a))
        print("Two distinct real roots are %.2f and %.2f" %(root1, root2))

    elif(dis == 0):
        root1 = root2 = -b / (2 * a)
        print("Two equal and real roots are %.2f and %.2f" %(root1, root2))

    elif(dis < 0):
        root1 = root2 = -b / (2 * a)
        imaginary = math.sqrt(-dis) / (2 * a)
        print("Two distinct complex roots are %.2f+%.2f and %.2f-%.2f" 
                         %(root1, imaginary, root2, imaginary))

# take inputs
a = int(input('Enter the value of a: '))
b = int(input('Enter the value of b: '))
c = int(input('Enter the value of c: '))

# calling function
edu_roots(a, b, c)

Output:-

Enter the value of a: 5
Enter the value of b: 10
Enter the value of c: 15
Two distinct complex roots are -1.00+1.41 and -1.00-1.41

Python Program to Solve Quadratic Equation using cmath

# Python program to find roots of quadratic equation

import cmath  #importing complex math-module

# take inputs
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
dis = (b**2) - (4*a*c)

# find roots of quadratic equation
root1 = (-b-cmath.sqrt(dis))/(2*a)
root2 = (-b+cmath.sqrt(dis))/(2*a)

# display result
print('The roots are {0} and {1}'.format(root1,root2))

Output:-

Enter the value of x: 2
Enter the value of y: 3
Enter the value of z: 7
The roots are (-0.75-1.713913650100261j) and (-0.75+1.7139136
50100261j)

Also See:- HCF or GCD of Two Numbers in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *