Write a program to compute the real roots of a quadratic equation

ax2 +bx +c =0

The roots are given by the equations

x1= -b + (sqrt(b2-4ac)/2a)

x2= -b – (sqrt(b2-4ac)/2a)

The program should request for the values of the constants a, b and c and print the values of x1 and x2. Use the following rules:

(a)    No solution, if both a and b are zero

(b)   There is only one root, if a=0 (x=-c/b)

(c)    There are no real roots, if b2-4ac is negative

(d)   Otherwise, there are two real roots

Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output message.

				
					import java.util.Scanner;

public class QuadraticEquationSolver {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Requesting input from the user
        System.out.print("Enter the values of a, b, and c: ");
        double a = scanner.nextDouble();
        double b = scanner.nextDouble();
        double c = scanner.nextDouble();

        // No solution if both a and b are zero
        if (a == 0 && b == 0) {
            System.out.println("No solution, since both a and b are zero.");
            return;
        }

        // Only one root if a is zero
        if (a == 0) {
            double x = -c / b;
            System.out.printf("There is only one root: x = %.2f\n", x);
            return;
        }

        // Calculate the discriminant
        double discriminant = b * b - 4 * a * c;

        // No real roots if discriminant is negative
        if (discriminant < 0) {
            System.out.println("There are no real roots, since the discriminant is negative.");
            return;
        }

        // Calculate the two real roots
        double x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
        double x2 = (-b - Math.sqrt(discriminant)) / (2 * a);

        // Output the roots
        System.out.printf("The roots of the quadratic equation are: x1 = %.2f and x2 = %.2f\n", x1, x2);
    }
}

				
			

Explanation:

  1. Input Request: The program starts by requesting the values of , , and from the user.
  2. No Solution: If both and are zero, it prints a message indicating that there is no solution.
  3. One Root: If is zero, it calculates and prints the single root =−/.
  4. Discriminant Calculation: It calculates the discriminant Δ=2−4.
  5. No Real Roots: If the discriminant is negative, it prints a message indicating that there are no real roots.
  6. Two Real Roots: If the discriminant is non-negative, it calculates the two roots using the quadratic formula and prints them.

Output:

Here are examples of the program’s output for different inputs:

  1. Both and are zero:

				
					Enter the values of a, b, and c: 0 0 5
No solution, since both a and b are zero.

				
			

Only one root:

				
					Enter the values of a, b, and c: 0 4 8
There is only one root: x = -2.00


				
			

No real roots:

				
					Enter the values of a, b, and c: 1 2 5
There are no real roots, since the discriminant is negative.


				
			

Two real roots:

				
					Enter the values of a, b, and c: 1 -3 2
The roots of the quadratic equation are: x1 = 2.00 and x2 = 1.00


				
			

To compile and run the program, you can use a Java compiler:

  1. Save the code in a file named QuadraticEquationSolver.java.
  2. Open a terminal and navigate to the directory containing the file.
  3. Compile the program using the javac compiler:
				
					javac QuadraticEquationSolver.java



				
			

Run the compiled program using the java interpreter:

				
					java QuadraticEquationSolver



				
			

You should see the output displayed as shown above.

Scroll to Top