# How to solve a Quadratic Equation in Java ?.

Updated on December 16, 2017

In this Java tutorial, you will learn how to solve Quadratic equation through a Java program. In this Java tutorial, you will encounter a video tutorial which will demonstrate how to solve Quadratic equation step-by-step. Below there is complete Java source code along with steps to implement the same. At last there is output of the program.

## Video tutorial - How to solve Quadratic Equation in Java ?.

Below is the video tutorial, which demonstrate how to solve Quadratic Equation in Java step-by-step.

```// Step 1 - Import Scanner class from the package
// java.util. Before using Scanner class nextDouble
// method, you have to import Scanner class.
import java.util.Scanner;

{
public static void main(String[] args)
{
// Step 2 - Create a Scanner class instance using
// new operator and calling Scanner constructor.
// The constructor takes in System.in inputstream
// as a parameter to it. System.in helps Scanner
// class to read user inputs from the Java console.
Scanner input = new Scanner(System.in);

// Step 3 - Create 5 variables of datatype double.
// These variables take in values of constants used in
// quadratic equation such as - a , b , c , d ,discriminant.
// All these variables are initialized to zero. These variables
// hold values entered by the user later.
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double discriminant = 0;

// Step 4 - Prompt the user to enter the value of constant a.
System.out.print("Enter the value of constant a : ");

// Step 5 - a holds the value entered by the user on Java console.
// Scanner class has a method by name nextDouble(). This method
// reads double value entered by user on the Java console and
// assign it to a variable.
a = input.nextDouble();

// Step 6 - Prompt the user to enter the value of constant b.
System.out.print("Enter the value of constant b : ");

// Step 7 - b holds the value entered by the user on Java console.
// Scanner class has a method by name nextDouble(). This method
// reads double value entered by user on the Java console and
// assign it to b variable.
b = input.nextDouble();

// Step 8 - Prompt the user to enter the value of constant c.
System.out.print("Enter the value of constant c : ");

// Step 9 - c holds the value entered by the user on Java console.
// Scanner class has a method by name nextDouble(). This method
// reads double value entered by user on the Java console and
// assign it to c variable.
c = input.nextDouble();

// Step 10 - Calculate discriminant of quadratic equation by using
// following formula -
// discriminant = (b * b - 4 * a * c)
discriminant = (b * b - 4 * a * c);

// Step 11 - Calculate the value of d by using following formula -
// d = square root of discriminant.
// Math class has a static method by name sqrt(). This method
// returns square root of number passed to it as parameter.
d = Math.sqrt(discriminant);

// Step 12 - Here, if discriminant comes out to be greater than
// quadratic equation has two roots calculated by formula -
// roots = + ( -b + d ) / ( 2.0 * a ))
// else roots of quadratic equation are not real numbers.
if (discriminant >= 0.0) {
System.out.println("First root of the Quadratic equation is : "
+(-b + d) / (2.0*a));

System.out.println("Second root of the Quadratic equation is : "
+(-b - d) / (2.0*a));
}
else
{
System.out.println("The roots are not real numbers.");
}
}
}```

## Steps to implement -

Step 1 - Import Scanner class from the package java.util. Before using Scanner class nextDouble method, you have to import Scanner class.

Step 2 - Create a Scanner class instance using new operator and calling Scanner constructor. The constructor takes in System.in inputstream as a parameter to it. System.in helps Scanner class to read user inputs from the Java console.

Step 3 - Create 5 variables of datatype double. These variables take in values of constants used in quadratic equation such as - a , b , c , d ,discriminant. All these variables are initialized to zero. These variables hold values entered by the user later.

Step 4 - Prompt the user to enter the value of constant a.

Step 5 - 'a' holds the value entered by the user on Java console. Scanner class has a method by name nextDouble(). This method reads double value entered by user on the Java console and assign it to 'a' variable.

Step 6 - Prompt the user to enter the value of constant b.

Step 7 - 'b' holds the value entered by the user on Java console. Scanner class has a method by name nextDouble(). This method reads double value entered by user on the Java console and assign it to 'b' variable.

Step 8 - Prompt the user to enter the value of constant c.

Step 9 - 'c' holds the value entered by the user on Java console. Scanner class has a method by name nextDouble(). This method reads double value entered by user on the Java console and assign it to 'c' variable.

Step 10 - Calculate discriminant of quadratic equation by using
following formula -
discriminant = (b * b - 4 * a * c)

Step 11 - Calculate the value of d by using following formula -
d = square root of discriminant.
Math class has a static method by name sqrt(). This method returns square root of number passed to it as parameter.

Step 12 - Here, if discriminant comes out to be greater than quadratic equation has two roots calculated by formula -
roots = + ( -b + d ) / ( 2.0 * a ))
else roots of quadratic equation are not real numbers.

## Output of the program -

Output of the program when the value of the discriminant is greater than 0.

Therefore,

discriminant = (b * b - 4 * a * c) = 6 * 6 - 4 * 4 * 2 = 4, which is greater than 0.

## Output of the program -

Output of the program when the value of the discriminant is equal to 0.

Therefore,

discriminant = (b * b - 4 * a * c) = 4 * 4 - 4 * 2 * 2 = 0, which is equal to 0.

## Output of the program -

Output of the program when the value of the discriminant is less than 0.

Therefore,

discriminant = (b * b - 4 * a * c) = 4 * 4 - 4 * 10 * 2 = -64, which is less than 0.

Thanks for reading the tutorial. In case you find any difficulty in solving Quadratic Equation in Java, please write into comments. The video tutorial demonstrates step by step execution of the program.

32

1

7