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 determinant of a quadratic equation. It specifies the nature of roots. That is,
- if determinant > 0, roots are real and different
- if determinant == 0, roots are real and equal
- if determinant < 0, roots are complex complex and different
Example: Java Program to Find Roots of a Quadratic Equation
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 determinant is calculated as b2 - 4ac.
Based on the value of the determinant, 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);