Python Program to find roots of a Quadratic Equation

Write a Python program to find Roots of a Quadratic Equation with an example. The mathematical representation of a Quadratic Equation is ax²+bx+c = 0. A Quadratic Equation can have two roots, and they depend entirely upon the discriminant. If discriminant > 0, then Two Distinct Real Roots exist for this equation

C Program to find Roots of a Quadratic Equation 3

If discriminant = 0, Two Equal and Real Roots exist.

C Program to find Roots of a Quadratic Equation 4

And if discriminant < 0, Two Distinct Complex Roots exist.

C Program to find Roots of a Quadratic Equation 5

Python Program to find roots of a Quadratic Equation using elif

This Python program allows users to enter three values for a, b, and c. By using those values, this code finds the roots of a quadratic equation using Elif Statement. Please refer to the Python elif Statement from our Python tutorial page.

import math

a = int(input("Please Enter a Value of a Quadratic Equation : "))
b = int(input("Please Enter b Value of a Quadratic Equation : "))
c = int(input("Please Enter c Value of a Quadratic Equation : "))

discriminant = (b * b) - (4 * a * c)

if(discriminant > 0):
    root1 = (-b + math.sqrt(discriminant) / (2 * a))
    root2 = (-b - math.sqrt(discriminant) / (2 * a))
    print("Two Distinct Real Roots Exists: root1 = %.2f and root2 = %.2f" %(root1, root2))
elif(discriminant == 0):
    root1 = root2 = -b / (2 * a)
    print("Two Equal and Real Roots Exists: root1 = %.2f and root2 = %.2f" %(root1, root2))
elif(discriminant < 0):
    root1 = root2 = -b / (2 * a)
    imaginary = math.sqrt(-discriminant) / (2 * a)
    print("Two Distinct Complex Roots Exists: root1 = %.2f+%.2f and root2 = %.2f-%.2f" %(root1, imaginary, root2, imaginary))
Python Program to find roots of a Quadratic Equation

Roots of a quadratic equation using functions

In the following program, we used the same logic (mathematical formula) that we specified in the first example. However, we separated the logic and placed in a user-defined function (findQuadRoot).

import math

def findQuadRoot(a, b, c):
disc = b * b - (4 * a * c)
sq = math.sqrt(abs(disc))

if disc > 0:
r1 = (-b + sq) / (2 * a)
r2 = (-b - sq) / (2 * a)
print('Two Distinct Real Roots Exists: %.2f and %.2f' %(r1, r2))
elif disc == 0:
r1 = r2 = -b / (2 * a)
print('Two Equal and Real Roots Exists: %.2f and %.2f' %(r1, r2))
else:
r1 = -b + 1j * sq / (2 * a)
r2 = -b - 1j * sq / (2 * a)
print('Two Distinct Complex Roots Exists: %.2f and %.2f' %(r1, r2))


a = 10
b = 40
c = 13
findQuadRoot(a, b, c)
Two Distinct Real Roots Exists: -0.36 and -3.64

Roots of a quadratic equation using cmath

This program is the simplest approach to finding the roots of a quadratic equation using the cmath module’s sqrt() function.

import cmath
a = 20
b = 50
c = -13

disc = (b ** 2) - (4 * a * c)
r1 = (-b + cmath.sqrt(disc)) / (2 * a)
r2 = (-b - cmath.sqrt(disc)) / (2 * a)
print('The roots of the Quadratic Equation are:', r1, r2)
The roots of the Quadratic Equation are: (0.2374474780643517+0j) (-2.7374474780643516+0j)

Also Read