Python program to find roots of a quadratic equation

Roots of a quadratic equation in Python

Here, in this page we will discuss the program to find the roots of a quadratic equation in python . In this python program, we will learn how to find the roots of a quadratic equation [ax2 + bx + c].

Roots of a quadratic equation in python

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-) [sqrt(pow(b,2)-4ac)] ] / 2a

 

Algorithm :

  • Calculate Discriminant (D)
    • D = b^2 – 4*a*c
  • If D>0 : Two real root exists.
  • If D=0 : Equal root exists.
  • If D<0 : Imaginary root exists.

 

Code in Python

Run
# Write a program to find roots of a quadratic equation in Python
import math
 
def findRoots(a, b, c):
 
    if a == 0:
        print("Invalid")
        return -1
        
    d = b * b - 4 * a * c
    sqrt_val = math.sqrt(abs(d))
 
    if d > 0:
        print("Roots are real and different ")
        print((-b + sqrt_val)/(2 * a))
        print((-b - sqrt_val)/(2 * a))
    elif d == 0:
        print("Roots are real and same")
        print(-b / (2*a))
    else:  # d<0
        print("Roots are complex")
        print(- b / (2*a), " + i", sqrt_val)
        print(- b / (2*a), " - i", sqrt_val)
 
 
# Driver Program
a = 1
b = 4
c = 4
 
# Function call
findRoots(a, b, c)

Output :

Roots are real and same
-2.0

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Python program to find roots of a quadratic equation”


  • Ganesh

    #quadratic equation: x^2 -2x -15
    a = 1
    b = -2
    c = -15

    x1 = (-b + (b ** 2 – 4 * a * c) ** 0.5)/ 2 * a
    x2 = (-b – (b ** 2 – 4 * a * c) ** 0.5)/2 * a

    print(x1)
    print(x2)


    • Ganesh

      #more clearly
      #quadratic equation: x^2 -2x -15
      a = 1
      b = -2
      c = -15
      x1 = (-b + (b ** 2 – 4 * a * c) ** 0.5)/ 2 * a
      x2 = (-b – (b ** 2 – 4 * a * c) ** 0.5)/2 * a
      print(x1)
      print(x2)


  • Om Prakash

    Code in Java :
    import java.util.Scanner;
    public class Quadratic_equation
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    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);
    System.out.println(root1);
    double root2 = (-b – Math.sqrt(determinant) )/(2*a);
    System.out.println(root2);
    }
    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
    }
    else
    {
    System.out.println("Equal roots: ");
    System.out.println(-b/2*a);
    System.out.println(-b/2*a);
    }
    }
    }