Write A Java Program to Calculate Root of Quadratic Equation
A quadratic equation is a second-degree polynomial equation in the form ax² + bx + c = 0. This calculator helps you write a Java program to find the roots of such equations.
Introduction
Quadratic equations are fundamental in algebra and have wide applications in physics, engineering, and computer science. The roots of a quadratic equation can be real or complex, depending on the discriminant.
In this guide, you'll learn how to write a Java program that calculates the roots of a quadratic equation. The program will handle all cases, including when the discriminant is positive, negative, or zero.
Quadratic Equation Formula
The general form of a quadratic equation is:
ax² + bx + c = 0
The roots of the equation can be found using the quadratic formula:
x = [-b ± √(b² - 4ac)] / (2a)
The discriminant (D) determines the nature of the roots:
- If D > 0: Two distinct real roots
- If D = 0: One real root (repeated)
- If D < 0: Two complex conjugate roots
Java Program to Calculate Roots
Here's a complete Java program that calculates the roots of a quadratic equation:
import java.util.Scanner;
import java.lang.Math;
public class QuadraticRoots {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Quadratic Equation Solver");
System.out.println("Enter coefficients a, b, and c:");
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
System.out.print("c: ");
double c = scanner.nextDouble();
double discriminant = b * b - 4 * a * c;
if (a == 0) {
System.out.println("This is not a quadratic equation (a cannot be zero).");
} else if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Two real and distinct roots:");
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("One real root (repeated):");
System.out.println("Root: " + root);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
System.out.println("Two complex conjugate roots:");
System.out.println("Root 1: " + realPart + " + " + imaginaryPart + "i");
System.out.println("Root 2: " + realPart + " - " + imaginaryPart + "i");
}
scanner.close();
}
}
The program prompts the user to enter coefficients a, b, and c. It then calculates the discriminant and determines the nature of the roots before displaying the results.
Example Calculation
Let's solve the quadratic equation x² - 5x + 6 = 0:
- Identify coefficients: a = 1, b = -5, c = 6
- Calculate discriminant: D = (-5)² - 4(1)(6) = 25 - 24 = 1
- Since D > 0, there are two distinct real roots
- Apply quadratic formula:
- Root 1 = [5 + √1]/2 = (5 + 1)/2 = 3
- Root 2 = [5 - √1]/2 = (5 - 1)/2 = 2
The roots of the equation are 3 and 2.
Frequently Asked Questions
- What is a quadratic equation?
- A quadratic equation is a second-degree polynomial equation in the form ax² + bx + c = 0.
- How do you find the roots of a quadratic equation?
- You can find the roots using the quadratic formula: x = [-b ± √(b² - 4ac)] / (2a).
- What does the discriminant tell you about the roots?
- The discriminant (b² - 4ac) determines the nature of the roots:
- Positive discriminant: Two distinct real roots
- Zero discriminant: One real root (repeated)
- Negative discriminant: Two complex conjugate roots
- Can a quadratic equation have complex roots?
- Yes, if the discriminant is negative, the equation will have two complex conjugate roots.
- What happens if a is zero in a quadratic equation?
- If a is zero, the equation is no longer quadratic but linear, and the solution is simply x = -c/b.