Find all the roots of a quadratic equation in C, C++, Java and Python | faceprep

Program to find all the roots of a quadratic equation is discussed here.

The general form of a quadratic equation is (ax^2 + bx + c = 0).

The highest degree in a quadratic equation is 2. Hence, a quadratic equation will have two roots.

Solving a quadratic equation

The formula to find the roots  of a quadratic equation is given as follows

x = [-b +/- sqrt(-b^2 – 4ac)]/2a 

The discriminant of the quadratic equation is

k = (b^2 – 4ac).

Depending upon the nature of the discriminant, the roots can be found in different ways.

  1. If the discriminant is positive, then there are two distinct real roots.
  2. If the discriminant is zero, then the two roots are equal.
  3. If the discriminant is negative, then there are two distinct complex roots.

Case 1: If the discriminant is positive, 

r1 = (-b + √k)/ 2a and r2 = (b + √k)/ 2a are the two roots.

Case 2: If the discriminant is zero, 

r1 = r2 = (-b / 2a) are the two roots.

Case 3: If the discriminant is negative, 

r1 = (-b +i √k)/ 2a and r2 = (b + i√k)/ 2a are the two roots.

For example, consider the following equation 

2x^2– 8x + 3 = 0.

a = 2, b = -8, c = 3

Discriminant value, k = b^2 – 4ac
                                                 = 8^2 – 4*(-8)*3
                                                 = 40

The discriminant value is positive. Hence, the roots are real and distinct.

r1 = (-b + √k)/ 2a
      = (8 + √40) /2*2
      = 2.3875

r2 = (b + √k)/ 2a
      = (-8 + √40) /2*2
      = -0.3875

r1 = 2.3875 and r2 = -0.3875 are the two roots.

Algorithm to find all the roots of a quadratic equation

1. Input the value of a, b, c.
2. Calculate k = b*b – 4*a*c
3. If (d < 0)
               Display “Roots are Imaginary, calculate r1 = (-b +i √k)/ 2a and r2 = (b + i√k)/ 2a.
     else if (d = 0)
                Display “Roots are Equal” and calculate r1 = r2 = (-b / 2*a) 
      else     
            Display “Roots are real and calculate r1 = -b + √d / 2*a and  r2 = -b – √d / 2*a
4. Print r1 and r2.
5. End the algorithm.

Program to find all the roots of a quadratic equation

// C program to find all the roots of a quadratic equation

#include <stdio.h>
#include <math.h>

int main()
{
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

printf(“Enter coefficients a, b and c: “);
scanf(“%lf %lf %lf”,&a, &b, &c);

discriminant = b*b-4*a*c;

// condition for real and different roots
if (discriminant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);

printf(“root1 = %.2lf and root2 = %.2lf”,root1 , root2);
}

//condition for real and equal roots
else if (discriminant == 0)
{
root1 = root2 = -b/(2*a);

printf(“root1 = root2 = %.2lf;”, root1);
}

// if roots are not real
else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
printf(“root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi”, realPart, imaginaryPart, realPart, imaginaryPart);
}

return 0;
}

// C++ program to find all the roots of a quadratic equation

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

cout << “Enter coefficients a, b and c: “;
cin >> a >> b >> c;

discriminant = b*b-4*a*c;

// condition for real and different roots
if (discriminant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);

cout << “root1 = ” << root1 << ” and root2 = ” << root2;
}

//condition for real and equal roots
else if (discriminant == 0)
{
root1 = root2 = -b/(2*a);

cout << “root1 = root2 = ” << root1;
}

// if roots are not real
else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
cout << “root1 = “<<realPart<< “+”<<imaginaryPart<<” and root2 = “<<realPart<< “+”<<imaginaryPart;
}

return 0;
}

//Java program to find all the roots of a quadratic equation

import java.util.*;
public class Main
{

public static void main(String[] args)
{
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
Scanner sc = new Scanner(System.in);
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
discriminant = b*b-4*a*c;

// condition for real and different roots
if (discriminant > 0)
{
// sqrt() function returns square root
root1 = (-b+Math.sqrt(discriminant))/(2*a);
root2 = (-b-Math.sqrt(discriminant))/(2*a);

System.out.print(“root1 = ” + root1 + “and root2 = ” + root2);
}

//condition for real and equal roots
else if (discriminant == 0)
{
root1 = root2 = -b/(2*a);
System.out.print(“root1 = root2 = ” + root1 );

}

// if roots are not real
else
{
realPart = -b/(2*a);
imaginaryPart = Math.sqrt(-discriminant)/(2*a);
System.out.print(“root1 = ” + realPart + “+” + imaginaryPart + “and root2 = ” + realPart + “+” + imaginaryPart);
}


}
}

 

# Python program to find all the roots of a quadratic equation

import math
print(“Enter co-efficients a, b and c : “)
a = int(input())
b = int(input())
c = int(input())
discriminant = b*b-4*a*c
# condition for real and different roots
if(discriminant > 0):
# sqrt() function returns square root
root1 = (-b+math.sqrt(discriminant))/(2*a)
root2 = (-b-math.sqrt(discriminant))/(2*a)

print(“root1 = “,root1,”and root2 = “,root2)

# condition for real and equal roots
elif(discriminant == 0):
root1 = root2 = -b/(2*a);
print(“root1 = root2 = “,root1 )

# if roots are not real
else:
realPart = -b/(2*a)
imaginaryPart = math.sqrt(-discriminant)/(2*a)
print(“root1 = “,realPart,”+”,imaginaryPart,”and root2 = “,realPart,”+”,imaginaryPart)

Output:

roots of a quadratic equation

Time complexity: O(1)