Python Examples

Python Program to Solve Quadratic Equation

Quadratic equations are used in calculating areas, calculating a product’s profit, or estimating an object’s speed. A quadratic equation is a second-degree equation. The standard form of the quadratic equation in python is written as px² + qx + r = 0. The coefficients in the above equation are p, q, r.

How do you do the Quadratic Equation in Python

The quadratic equation is written as (-b ± (b ** 2 – 4 * a * c) ** 0.5) / 2 * a. The standard form of the quadratic equation is given as ax^2+bx+c=0, a, b and c are real numbers and a ≠ 0

quadratic formula in python


How do you find the Roots of Quadratic Equation in Python

When finding quadratic  equation in python, one must consider the points discussed below:

The roots of a quadratic equation can be classified as: 

  • If b*b < 4*a*c, then roots are complex
  • If b*b == 4*a*c, then roots are real, and both roots are the same.
  • If b*b > 4*a*c, then roots are real and different

For example:

Input : p= 1, q= 2, r= 1 

Output : 

Roots are real and same


Input : x = 2, y = 2, z = 1

Output :

Roots are complex

-0.5 + i 2.0

-0.5 – i 2.0

Input : x = 1, y = 10, z = -24 

Output : 

Roots are real and different



Source Code:


#The following program is used to find out the roots of the quadratic equation 

import math 

def equationroots( x, y, z): 

      discri = y * y - 4 * x * z 

    sqrtval = math.sqrt(abs(discri)) 


    # checking condition for discriminant

    if discri > 0: 

        print(" real and different roots ") 

        print((-y + sqrtval)/(2 * x)) 

        print((-y - sqrtval)/(2 * x)) 


    elif discri == 0: 

        print(" real and same roots") 

        print(-y / (2 * x)) 


    # when discriminant is less than 0


        print("Complex Roots") 

        print(- y / (2 * x), " + i", sqrt_val) 

        print(- y / (2 * x), " - i", sqrt_val) 


# Driver Program 

x = 1

y = 10

z = -24


if x == 0: 

        print("Input correct quadratic equation") 


    equationroots(x, y, z)


real and different roots



How do you write a Python Program to Solve Quadratic Equation

The program given below generates the quadratic equation when the a, b, c coefficients are known. 


import c math

x = 1

y = 5

z = 6

# calculate the discriminant

w = (y**2) - (4*x*z)

# find two solutions

sol1 = (-y-cmath.sqrt(w))/(2*x)

sol2 = (-y+cmath.sqrt(w))/(2*x)

print('The solution are {0} and {1}'.format(sol1,sol2))


Enter x: 1

Enter y: 5

Enter z: 6

The solutions are (-3+0j) and (-2+0j)

What is the quadratic function formula?

The standard formula of a quadratic equation in Python is ax^2+bx+c=0.

In the above equation, a,b,c are the coefficients and real numbers and, a is not equal to zero. If a=0, then it will not be a valid quadratic equation. 

The discriminant of the quadratic formula equation can be written as b^2-4ac

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

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.