Here’s a Java program that displays the roots of a quadratic equation of the form ax² + bx = 0
(note: no constant term c, so it's a special case). It calculates the discriminant D and describes
the nature of the roots without using Scanner (i.e., input values are hardcoded):
✅ Java Code:
java
CopyEdit
public class QuadraticRoots {
public static void main(String[] args) {
// Coefficients of the quadratic equation: ax² + bx = 0
double a = 2; // Change as needed
double b = 4; // Change as needed
System.out.println("Given equation: " + a + "x² + " + b + "x = 0");
// Discriminant formula: D = b² - 4ac, but c = 0 here
double D = b * b;
System.out.println("Discriminant (D) = " + D);
// Roots using: x(ax + b) = 0 → x = 0 or ax + b = 0
double root1 = 0;
double root2 = -b / a;
System.out.println("Roots are: x = " + root1 + " and x = " +
root2);
// Nature of roots
if (D > 0) {
System.out.println("Nature of Roots: Real and Unequal");
} else if (D == 0) {
System.out.println("Nature of Roots: Real and Equal");
} else {
System.out.println("Nature of Roots: Complex (Imaginary)");
}
}
}
📌 Sample Output (for a = 2, b = 4):
mathematica
CopyEdit
Given equation: 2.0x² + 4.0x = 0
Discriminant (D) = 16.0
Roots are: x = 0.0 and x = -2.0
Nature of Roots: Real and Unequal