# Java Program to Find the Roots of Quadratic Equation

In the previous article, we have seen Java Program to Find the Simple Interest

In this article we are going to see how to find the roots of quadratic equation using Java programming language.

## Java Program to Find the Roots of Quadratic Equation

Before Jumping into the program directly let’s see how to Find the Roots of Quadratic Equation.

Explanation:

We know the general form of quadratic equation is ax2 + bx + c = 0

Then the roots of quadratic equation is -b+√(b2-4ac)/2a & -b-√(b2-4ac)/2a

Here (b2-4ac) is called the determinant say “D”

• If D>0, then roots are real and unequal i.e. -b+√(b2-4ac)/2a & -b-√(b2-4ac)/2a
• If D=0 then roots are real and equal i.e. -b/2a & -b/2a
• If D<0 then roots are imaginary and unequal i.e. -b/2a + i√(b2-4ac)/2a & -b/2a – i√(b2-4ac)/2a

Example:

Let  a=3, b=10 and c =5

D = b2-4ac = 100-60 = 40

Since D > 0,

Hence the roots are -5/3 + i√20/3 & -5/3 – i√20/3

Let’s see different ways to find the roots of quadratic equation.

### Method-1: Java Program to Find the Roots of Quadratic Equation By Using Static Input Value

Approach:

• Declare an int variable say ‘a’ and assign the value to it, which holds the value of coefficient a.
• Declare an int variable say ‘b’ and assign the value to it, which holds the value of coefficient b.
• Declare an int variable say ‘c’ and assign the value to it, which holds the value of coefficient c.
• Find the discriminant of the quadratic equation using the formula D = b2-4ac.
• Now if If D>0, then roots are real and unequal i.e. -b+√(b2-4ac)/2a & -b-√(b2-4ac)/2a
• If D=0 then roots are real and equal i.e. -b/2a & -b/2a
• If D<0 then roots are imaginary and unequal i.e. -b/2a + i√(b2-4ac)/2a & -b/2a – i√(b2-4ac)/2a
• Print the result.

Program:

import java.util.*;
class Main
{
public static void main(String [] args)
{
//value of coefficient of a,b,c declared
double a = 2;
double b = -3;
double c = 2;
//formula to find the discriminant
double D =  (b*b)-(4*a*c);
//finding roots
if(D>0)
System.out.println("Roots are " + (-b+Math.sqrt(D))/(2*a) + " and " + (-b-Math.sqrt(D))/(2*a));
else if (D == 0)
System.out.println("Roots are " + -b/(2*a));
else
System.out.println("Roots are " + -b/(2*a) + "+i" + Math.sqrt(-D)/(2*a) + " and "+ -b/(2*a) + "-i" + Math.sqrt(-D)/(2*a));
}
}

Output:

Roots are 0.75+i0.6614378277661477 and 0.75-i0.6614378277661477

### Method-2: Java Program to Find the Roots of Quadratic Equation By Using User Input Value

Approach:

• Declare an int variable say ‘a’ which holds the value of coefficient a.
• Declare an int variable say ‘b’ which holds the value of coefficient b.
• Declare an int variable say ‘c’ which holds the value of coefficient c.
• Take the user input of values of a, b, c.
• Find the discriminant of the quadratic equation using the formula D = b2-4ac.
• Now if If D>0, then roots are real and unequal i.e. -b+√(b2-4ac)/2a & -b-√(b2-4ac)/2a
• If D=0 then roots are real and equal i.e. -b/2a & -b/2a
• If D<0 then roots are imaginary and unequal i.e. -b/2a + i√(b2-4ac)/2a & -b/2a – i√(b2-4ac)/2a
• Print the result.

Program:

import java.util.*;
class Main
{
public static void main(String [] args)
{
//taking user input of values of coefficient of a,b,c
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of coefficient a:");
double a = s.nextDouble();
System.out.println("Enter the value of coefficient b:");
double b =  s.nextDouble();
System.out.println("Enter the value of coefficient c:");
double c = s.nextDouble();

//formula to find the discriminant
double D =  (b*b)-(4*a*c);
//finding roots
if(D>0)
System.out.println("Roots are " + (-b+Math.sqrt(D))/(2*a) + " and " + (-b-Math.sqrt(D))/(2*a));
else if (D == 0)
System.out.println("Roots are " + -b/(2*a));
else
System.out.println("Roots are " + -b/(2*a) + "+i" + Math.sqrt(-D)/(2*a) + " and "+ -b/(2*a) + "-i" + Math.sqrt(-D)/(2*a));
}
}

Output:

Case-1
Enter the value of coefficient a:
1
Enter the value of coefficient b:
1
Enter the value of coefficient c:
1
Roots are -0.5+i0.8660254037844386 and -0.5-i0.8660254037844386

Case-2
Enter the value of coeffecient a:
1
Enter the value of coeffecient b:
2
Enter the value of coeffecient c:
1
Roots are -1.0

Case-3
Enter the value of coeffecient a:
1
Enter the value of coeffecient b:
3
Enter the value of coeffecient c:
2
Roots are -1.0 and -2.0

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: