C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Solve Quadratic Equation for Real & Imaginary Roots

import java.util.Scanner;

 

class QuadraticEquationSolver {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter coefficient a: ");

        double a = sc.nextDouble();

 

        System.out.print("Enter coefficient b: ");

        double b = sc.nextDouble();

 

        System.out.print("Enter coefficient c: ");

        double c = sc.nextDouble();

 

        double discriminant = (b * b) - (4 * a * c);

 

        System.out.println("\nDiscriminant = " + discriminant);

 

        if (discriminant > 0) {

            // Real and distinct roots

            double r1 = (-b + Math.sqrt(discriminant)) / (2 * a);

            double r2 = (-b - Math.sqrt(discriminant)) / (2 * a);

 

            System.out.printf("Roots are Real and Distinct:%n");

            System.out.printf("Root 1 = %.2f%n", r1);

            System.out.printf("Root 2 = %.2f%n", r2);

 

        } else if (discriminant == 0) {

            // Real and equal roots

            double r = -b / (2 * a);

 

            System.out.println("Roots are Real and Equal:");

            System.out.printf("Root = %.2f%n", r);

 

        } else {

            // Imaginary roots

            double realPart = -b / (2 * a);

            double imagPart = Math.sqrt(-discriminant) / (2 * a);

 

            System.out.println("Roots are Imaginary:");

            System.out.printf("Root 1 = %.2f + %.2fi%n", realPart, imagPart);

            System.out.printf("Root 2 = %.2f - %.2fi%n", realPart, imagPart);

        }

 

        sc.close();

    }

}

Output

OUTPUT 1: (Real & Distinct Roots)
 INPUT 
Enter coefficient a: 1
Enter coefficient b: -3
Enter coefficient c: 2

OUTPUT 
Discriminant = 1.0
Roots are Real and Distinct:
Root 1 = 2.00
Root 2 = 1.00

OUTPUT 2:(Real & Equal Roots)
 INPUT 
Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 1

OUTPUT 
Discriminant = 0.0
Roots are Real and Equal:
Root = -1.00

OUTPUT 3: (Imaginary / Complex Roots)
 INPUT 
Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 5

OUTPUT 
Discriminant = -16.0
Roots are Imaginary:
Root 1 = -1.00 + 2.00i
Root 2 = -1.00 - 2.00i
 

Explanation

  1. Input Coefficients

          The program reads a, b, and c from the user for the equation:

                           ax2 + bx + c = 0

  1. Compute Discriminant

           double discriminant = (b * b) - (4 * a * c);

                           Java Programs

           The discriminant determines the type of roots.

  1. Case 1 → Real & Distinct Roots (D > 0)

                         Java Programs

           Both roots are different real numbers.

  1. Case 2 → Real & Equal Roots (D = 0)

                         Java Programs

           Both roots are equal.

  1. Case 3 → Imaginary Roots (D < 0)

                         Java Programs

           The program prints roots as complex numbers:

               realPart ± imagPart i

 

Key Concepts Used

Quadratic formula
Discriminant
Math.sqrt()
Handling complex (imaginary) numbers
if–else decision making
Formatted output using printf