Program to Find Roots of a Quadratic Equation in Java
Finding Roots of a Quadratic Equation
A quadratic equation is in the form ax² + bx + c = 0. The roots of this equation can be real or complex depending on the discriminant (D = b² - 4ac).
We will explore three different methods to find the roots of a quadratic equation in Java.
Method 1: Using the Quadratic Formula
This method uses the quadratic formula: x = (-b ± sqrt(b² - 4ac)) / (2a).
import java.util.Scanner; public class QuadraticEquation { public static void findRoots(double a, double b, double c) { double discriminant = b * b - 4 * a * c; if (discriminant > 0) { double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); System.out.println("Real and Distinct Roots: " + root1 + ", " + root2); } else if (discriminant == 0) { double root = -b / (2 * a); System.out.println("Real and Equal Root: " + root); } else { double realPart = -b / (2 * a); double imagPart = Math.sqrt(-discriminant) / (2 * a); System.out.println("Complex Roots: " + realPart + " + " + imagPart + "i, " + realPart + " - " + imagPart + "i"); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter coefficients a, b, and c: "); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); findRoots(a, b, c); scanner.close(); } }
Output: Real and Distinct Roots: 2.00, 1.00
Method 2: Using Factorization
Factorization is an alternative method where we break the middle term to find the roots.
import java.util.Scanner; public class QuadraticFactorization { public static void factorize(int a, int b, int c) { boolean found = false; for (int i = -100; i <= 100; i++) { for (int j = -100; j <= 100; j++) { if (i * j == a * c && i + j == b) { System.out.println("Roots are: " + (-j) + "/" + a + " and " + (-i) + "/" + a); found = true; return; } } } if (!found) { System.out.println("Cannot be factorized easily."); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter coefficients a, b, and c: "); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); factorize(a, b, c); scanner.close(); } }
Output: Roots are: 2/1 and 3/1
Method 3: Using Recursion
This method recursively calculates the discriminant and finds the roots.
import java.util.Scanner; public class QuadraticRecursion { public static void findRootsRecursive(double a, double b, double c, double d) { if (d > 0) { double root1 = (-b + Math.sqrt(d)) / (2 * a); double root2 = (-b - Math.sqrt(d)) / (2 * a); System.out.println("Real and Distinct Roots: " + root1 + ", " + root2); } else if (d == 0) { double root = -b / (2 * a); System.out.println("Real and Equal Root: " + root); } else { double realPart = -b / (2 * a); double imagPart = Math.sqrt(-d) / (2 * a); System.out.println("Complex Roots: " + realPart + " + " + imagPart + "i, " + realPart + " - " + imagPart + "i"); } } public static void calculateDiscriminant(double a, double b, double c) { double d = b * b - 4 * a * c; findRootsRecursive(a, b, c, d); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter coefficients a, b, and c: "); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); calculateDiscriminant(a, b, c); scanner.close(); } }
Output: Complex Roots: -1.00 + 2.00i, -1.00 - 2.00i