# Java Program to find Roots of a Quadratic Equation

Write a Java program to find the Roots of a Quadratic Equation with an example. The mathematical representation of a Quadratic Equation is ax²+bx+c = 0. A Quadratic Equation has two roots, and they depend entirely upon the discriminant. If discriminant > 0, then two Distinct Real Roots exist for this equation.

If discriminant < 0, then Two Distinct Complex Roots exist.

And If discriminant = 0, then Two Equal and Real Roots exist.

## Java Program to find Roots of a Quadratic Equation using Else If

This Java program allows users to enter three values for a, b, and c. Next, this program returns the roots of a quadratic equation using the Else If Statement.

```import java.util.Scanner;

private static Scanner sc;
public static void main(String[] args)
{
double a, b, c;
double root1, root2, imaginary, discriminant;
sc = new Scanner(System.in);

System.out.print(" Please Enter the Values of a, b, c of Quadratic Equation : ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();

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

if(discriminant > 0)
{
root1 = (-b + Math.sqrt(discriminant) / (2 * a));
root2 = (-b - Math.sqrt(discriminant) / (2 * a));
System.out.println("\n Two Distinct Real Roots Exists: root1 = " + root1 + " and root2 = " + root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
System.out.println("\n Two Equal and Real Roots Exists: root1 = " + root1 + " and root2 = " + root2);
}
else if(discriminant < 0)
{
root1 = root2 = -b / (2 * a);
imaginary = Math.sqrt(-discriminant) / (2 * a);
System.out.println("\n Two Distinct Complex Roots Exists: root1 = " +
root1 + " + " + imaginary + " and root2 = " + root2 +" - " +imaginary);
}
}
}```
``````Please Enter the Values of a, b, c of Quadratic Equation : 2
3
5

Two Distinct Complex Roots Exists: root1 = -0.75 + 1.3919410907075054 and root2 = -0.75 - 1.3919410907075054``````

User entered Java Values are 2 3 5. It means a = 2, b = 3, c = 5 and the equation is 2x²+3x+5 = 0

discriminant = (b * b) – (4 * a *c) => (3 * 3) – (4 * 2 * 5)
discriminant = (9) – (40) = -31

It means discriminant < 0 so
root1 = root2 = -b / (2 * a) => -3 / (2 * 2)
root1 = root2 = -0.75

imaginary = sqrt(-discriminant) / (2 * a)
imaginary = sqrt(- -31) / (2 * 2) => 5.567 / 4
imaginary = 1.3919

root1= root1+imaginary = -0.75 + 1.3919

root2= root2-imaginary. = -0.75 – 1.3919

## Java Program to find Roots of a Quadratic Equation using Switch Case

This program to display the roots of a quadratic equation is the same as above, but this time we are using the Switch case.

```import java.util.Scanner;

private static Scanner sc;
public static void main(String[] args)
{
double a, b, c;
sc = new Scanner(System.in);

System.out.print(" Please Enter the Values of a, b, c of Quadratic Equation : ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();

}
public static void QuadraticEquation(double a, double b, double c)
{
double root1, root2, imaginary, discriminant;
discriminant = (b * b) - (4 * a *c);

if(discriminant > 0)
{
root1 = (-b + Math.sqrt(discriminant) / (2 * a));
root2 = (-b - Math.sqrt(discriminant) / (2 * a));
System.out.println("\n Two Distinct Real Roots Exists: root1 = " + root1 + " and root2 = " + root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
System.out.println("\n Two Equal and Real Roots Exists: root1 = " + root1 + " and root2 = " + root2);
}
else if(discriminant < 0)
{
root1 = root2 = -b / (2 * a);
imaginary = Math.sqrt(-discriminant) / (2 * a);
System.out.println("\n Two Distinct Complex Roots Exists: root1 = " + root1 +
" + " + imaginary + " and root2 = " + root2 +" - " +imaginary);
}
}
}```
`````` Please Enter the Values of a, b, c of Quadratic Equation : 10 15 -25

Two Distinct Real Roots Exists: root1 = -13.25 and root2 = -16.75``````

In this Java Program to find the Roots of a Quadratic Equation, the User enters Values 10, 15, and -25. It means a = 10, b = 15, c = -25 and the Quadratic equation is 10x²+15x-25 = 0

discriminant = (b * b) – (4 * a *c) => (15 * 15) – (4 * 10 *(-25))
= 225 + 1000 = 1225

It means discriminant > 0 so
root1 = (-b + sqrt(discriminant) / (2 * a))
= (-15 + sqrt(1225) / (2 * 10)) => (-15 + 35 / (20))
=  -15 + 1.75 = -13.25

root2 = (-b – sqrt(discriminant) / (2 * a))
= (-15 – sqrt(1225) / (2 * 10)) => (-15 – 35 / (20))
=  -15 – 1.75 = -16.75