# Program to implement a Quadratic equation in Java

In this program, you will learn to find all roots of a quadratic equation in Java.

Also, you can use the Java compiler to compile a program.

## Program to implement all Roots of a Quadratic equation in java

The standard form of equation is:

```ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
```

The term `b2-4ac` is known as the determinant of an equation. The determinant tells the nature of the roots.

• If the determinant is greater than 0, the roots are real and different.
• And determinant is equal to 0, the roots are real and equal.
• If the determinant is less than 0, the roots are complex and different.
##### SOURCE CODE.
``````import java.util.*;
{
public static void main(String args[])
{
int e,a,b,c;
double l,x,y;
Scanner s= new Scanner(System.in);
System.out.println("Enter value a,b,c:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();

e=b*b-4*a*c;
if(e<0)
{
System.out.println("NOT REAL");
}
else if(e == 0)
{
x = -b / (2.0 * a);
System.out.println("The root is " + x);
}
else
{
l=Math.sqrt(e);
x= (-b+l)/(2*a);
y= (-b-l)/(2*a);
System.out.println("equation= "+x+" & "+y);
}
}// END OF MAIN
}//END OF CLASS	``````

#### OUTPUT.

Enter value a,b,c:
1
4
4
The root is -2.0

#### OR

Enter value a,b,c:
1
5
2
equation= -0.4384471871911697 & -4.561552812808831

Also view: – Command-Line program find the Largest number

I am Ashish- a Developer live in Mumbai.

View all posts by Ashishkumar Vishwakarma →

### 3 Comments on “Program to implement a Quadratic equation in Java”

1. Cecil Porter says:

Thanks for the info 🙂