# Java Program to Find all Roots of a Quadratic Equation

To understand this example, you should have the knowledge of the following Java programming topics:

The standard form of a quadratic equation is:

``ax2 + bx + c = 0``

Here, a, b, and c are real numbers and a can't be equal to 0.

We can calculate the root of a quadratic by using the formula:

``x = (-b ± √(b2-4ac)) / (2a)``

The `±` sign indicates that there will be two roots:

``````root1 = (-b + √(b2-4ac)) / (2a)
root1 = (-b - √(b2-4ac)) / (2a)``````

The term `b2-4ac` is known as the discriminant of a quadratic equation. It specifies the nature of roots. That is,

• if discriminant > 0, roots are real and different
• if discriminant == 0, roots are real and equal
• if discriminant < 0, roots are complex and different

## Example: Java Program to Find Roots of a Quadratic Equation

``````public class Main {

public static void main(String[] args) {

// value a, b, and c
double a = 2.3, b = 4, c = 5.6;
double root1, root2;

// calculate the discriminant (b2 - 4ac)
double discriminant = b * b - 4 * a * c;

// check if discriminant is greater than 0
if (discriminant > 0) {

// two real and distinct roots
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
}

// check if discriminant is equal to 0
else if (discriminant == 0) {

// two real and equal roots
// discriminant is equal to 0
// so -b + 0 == -b
root1 = root2 = -b / (2 * a);
System.out.format("root1 = root2 = %.2f;", root1);
}

// if discriminant is less than zero
else {

// roots are complex number and distinct
double real = -b / (2 * a);
double imaginary = Math.sqrt(-discriminant) / (2 * a);
System.out.format("root1 = %.2f+%.2fi", real, imaginary);
System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}``````

Output

`root1 = -0.87+1.30i and root2 = -0.87-1.30i`

In the above program, the coefficients a, b, and c are set to 2.3, 4, and 5.6 respectively. Then, the `discriminant` is calculated as `b2` `- 4ac`.

Based on the value of the discriminant, the roots are calculated as given in the formula above. Notice we've used library function Math.sqrt() to calculate the square root of a number.

We have used the `format()` method to print the calculated roots.

The `format()` function can also be replaced by `printf()` as:

``System.out.printf("root1 = root2 = %.2f;", root1);``

Before we wrap up, let’s put your knowledge of Java Program to Find all Roots of a Quadratic Equation to the test! Can you solve the following challenge?

Challenge:

Write a function to find the roots of a quadratic equation.

• The formula to find the roots of a quadratic equation is: `x = [-b ± sqrt(b^2 - 4ac)] / 2a`.
• Return the roots of a quadratic equation with coefficients `a`, `b`, and `c` as an array.
• For example, if `a = 1`, `b = -5` and `c = 6`, the expected output is `{3, 2}`.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

• Interactive Courses
• Certificates
• AI Help
• 2000+ Challenges