Java Program to find Roots of a Quadratic Equation

Write a Java program to find Roots of a Quadratic Equation with 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 exists for this equation.

Java Program to find Roots of a Quadratic Equation 3

If discriminant < 0, then Two Distinct Complex Roots exists

Java Program to find Roots of a Quadratic Equation 5

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

Java Program to find Roots of a Quadratic Equation 4

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 Java program returns roots of a quadratic equation using Else If Statement.

// Java Program to find Roots of a Quadratic Equation
import java.util.Scanner;

public class QuadraticEquation1 {
	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);
	  	}		
	}
}

Java Quadratic equation roots output

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 Quadratic 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
root1 = -0.75 + 1.3919

root2 = root2 – imaginary
root2 = -0.75 – 1.3919

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

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

// Java Program to find Roots of a Quadratic Equation
import java.util.Scanner;

public class QuadraticEquation2 {
	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();
		
		QuadraticEquation(a, b, c);
	}
	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 Roots of a Quadratic Equation, User entered Values are 10 15 -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))
discriminant = 225 + 1000 = 1225

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

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