# Find Roots of Quadratic Equation Java Program

## Java Program To Find Roots of a Quadratic Equation

Learn How To Find Roots of a Quadratic Equation in Java Programming Language. This Java Program To Compute Roots of Quadratic Equation makes use of **If – Else** Block.

#### What is a Quadratic Equation?

It is a term used in Elementary Algebra. The Standard Form of a Quadratic Equation is **ax ^{2} + bx + c = 0, **where

**a**,

**b**,

**c**are constant values which cannot be changed and

**x**is a variable entity.

#### Quadratic Equation Formula

**h = −b ± √(b ^{2 }− 4ac) / 2a**

#### Conditions For Discriminants

- If
**b**, One Real Solution is possible^{2 }− 4ac = 0 - If
**b**is^{2 }− 4ac**Positive**, Two Real Solutions are possible - If
**b**is^{2 }− 4ac**Negative**, Two Complex Solutions are possible

The** java.lang.*** package consists of **Math.sqrt()** method which helps to calculate the Square Root of the Discriminant.

**Also Read: Java Program To Draw A Circle using Graphics**

#### Java Program To Find Roots of a Quadratic Equation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.io.*; import java.util.*; import java.lang.*; class quadratic_equation { public static void main(String args[]) { double x, y, z; double root1, root2; double discriminant, sqr; System.out.println("\nEnter The Values"); Scanner sc = new Scanner(System.in); System.out.println("\nX:\t"); x = sc.nextFloat(); System.out.println("\nY:\t"); y = sc.nextFloat(); System.out.println("\nZ:\t"); z = sc.nextFloat(); discriminant = y*y - 4*x*z; sqr = Math.sqrt(discriminant); if(discriminant<0) { System.out.println("\nRoots Are Imaginary\n"); } else { root1 = (-y + sqr) / (2*x); root2 = (-y - sqr) / (2*x); System.out.println("\nRoot 1 = " + root1 + "\n"); System.out.println("\nRoot 2 = " + root2 + "\n"); } } } |

**Also Read: Java Code To Convert Numbers To Words**

#### Output

If you have any compilation errors or doubts in this Program to Find Roots of a Quadratic Equation in Java Programming Language, let us know about in the Comment Section below.