Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python program to find roots of a quadratic equation

Roots of a quadratic equation

In this python program, we will learn how to find the roots of a quadratic equation[ax2 + bx + c]. When we try to solve the quadratic equation we find the root of the equation. Mainly roots of the quadratic equation are represented by parabola in 3 different patterns like

  • No Real Roots
  • One Real Root
  • Two Real Roots

When we solve the equation we get 3 conditions mentioned above using this formula:-
X = [-b (+or-)[Squareroot(pow(b,2)-4ac)]]/2a


Python program to find roots of quadratic equation


  • Step 1:- Start.
  • Step 2:- Take user inputs for a,b and c.
  • Step 3:- Check if the value of a is not zero.
  • Step 4:- Calculate Functions value ie, b*b – a*a*c.
  • Step 5:- Find the square root of the function.
  • Step 6:-if the value is greater than zero print Two real roots and value of roots.
  • Step 7:- If the value is equal to zero Print one real root and print the value.
  • Step 8:- If both the condition are false print no real roots and print values.

Python program to find the square root of a quadratic equation

#import math library
import math
#take user inputs
a = int(input('Enter value of a :'))
b = int(input('Enter value of b :'))
c = int(input('Enter value of c :'))
#check for value of a
if a == 0: 
    print("a cannot be zero") 
#if a is greater than 0
    #calculate value of Function
    val = b**2 - 4 * a * c 
    root = math.sqrt(abs(val)) 
    #Check for roots and print according to their nature
    if val > 0: 
        print("Two Real Roots") 
        print((-b + root)/(2 * a)) 
        print((-b - root)/(2 * a)) 
    elif val == 0: 
        print("One Real Root") 
        print(-b / (2*a)) 
        print("No Real Root") 
        print(- b / (2*a) , " + i", root) 
        print(- b / (2*a) , " - i", root)
Enter value of a :1
Enter value of b :-7
Enter value of c :12
Two Real Roots

One comment on “Python program to find roots of a quadratic equation”

  • Om Prakash

    Code in Java :
    import java.util.Scanner;
    public class Quadratic_equation
    public static void main(String[] args)
    Scanner sc = new Scanner(;
    System.out.print(“Enter a:”);
    int a = sc.nextInt();
    System.out.print(“Enter b:”);
    int b = sc.nextInt();
    System.out.print(“Enter c:”);
    int c = sc.nextInt();
    int determinant = b*b – 4*a*c;
    if(determinant > 0)
    System.out.println(“Real roots: “);
    double root1 = (-b + Math.sqrt(determinant) )/(2*a);
    double root2 = (-b – Math.sqrt(determinant) )/(2*a);
    else if(determinant < 0)
    System.out.println("Imaginary roots: ");
    double root1 = -b/(2*a);
    double rootImg1 = Math.abs(Math.sqrt(determinant));
    System.out.println(root1 +"+i"+rootImg1);
    System.out.println(root1 +"-i"+rootImg1); // Imaginary root2
    System.out.println("Equal roots: ");