Table of contents
1.
Introduction
2.
Conditions for a Quadratic Equation
3.
A Mathematical Formula for Finding the Roots of a Quadratic Equation
4.
The Roots of the Quadratic Equations Are
5.
Flowchart for Solving a Quadratic Equation in Java  
5.1.
Step-by-Step Logic  
5.2.
Flowchart Representation  
6.
Code to Find Roots of a Quadratic Equation
6.1.
Example Run
7.
Time and Space Complexity
7.1.
Time Complexity
7.2.
Space Complexity
8.
Frequently Asked Questions
8.1.
What happens if the coefficient ‘a’ is zero?
8.2.
Can a quadratic equation have only one root?
8.3.
What is the significance of the discriminant in a quadratic equation?
9.
Conclusion
Last Updated: Mar 9, 2025
Easy

Java Program to Find all Roots of a Quadratic Equation

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A quadratic equation has the standard form ax² + bx + c = 0, where a, b, and c are constants. The roots of a quadratic equation can be found using the quadratic formula. In Java, we can implement a program to compute these roots by considering real and complex solutions. 

Java Program to Find all Roots of a Quadratic Equation

This article will discuss the approach to finding all roots of a quadratic equation using Java, along with examples for better understanding.

Conditions for a Quadratic Equation

For an equation to be quadratic, it must meet the following conditions:

  1. The highest power of x must be 2.
     
  2. The coefficient a (the term multiplied by x²) should not be zero, i.e., a ≠ 0. If a = 0, the equation becomes linear instead of quadratic.
     
  3. The equation should have three terms in standard form: ax² + bx + c = 0.

A Mathematical Formula for Finding the Roots of a Quadratic Equation

The roots (solutions) of a quadratic equation are found using the quadratic formula:

Formula

Here:

  • b² - 4ac is called the discriminant (D).
     
  • The square root of the discriminant determines the nature of the roots.
     
  • The Â± sign means there are generally two possible values for x.

The Roots of the Quadratic Equations Are

The nature of the roots depends on the value of the discriminant (D = b² - 4ac):

  1. D > 0 → Two distinct real roots.
     
  2. D = 0 → Two equal real roots.
     
  3. D < 0 → No real roots (complex roots exist).

Flowchart for Solving a Quadratic Equation in Java  

Before writing the Java program, it's very important to understand the step-by-step process involved in solving a quadratic equation. A flowchart helps visualize the logic behind the program, making it easier to understand. Let’s take a look at the structured breakdown of how the program will work:  

Step-by-Step Logic  

1. Start the program.  
 

2. Take input values for coefficients a, b, & c from the user.  
 

3. Calculate the discriminant using the formula:  

   \[
   D = b^2 - 4ac
   \]


4. Check the discriminant value:  

   - If D > 0, the equation has two distinct real roots.  
 

   - If D == 0, the equation has one real root (repeated root).  
 

   - If D < 0, the equation has complex roots (no real solution).  
 

5. Compute the roots using the quadratic formula:  

   \[
   x = \frac{-b \pm \sqrt{D}}{2a}
   \]


6. Display the results based on the discriminant value.  
 

7. End the program.  

Flowchart Representation  

Here’s a simple flowchart to visualize the logic:  

Formula

Code to Find Roots of a Quadratic Equation

Below is a Java program that takes input values for a, b, and c, calculates the discriminant, and finds the roots accordingly.

import java.util.Scanner;

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

        // Taking inputs
        System.out.print("Enter value of a: ");
        double a = scanner.nextDouble();
        System.out.print("Enter value of b: ");
        double b = scanner.nextDouble();
        System.out.print("Enter value of c: ");
        double c = scanner.nextDouble();

        // Closing the scanner
        scanner.close();

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

        // Check conditions for the nature of roots
        if (D > 0) {
            double root1 = (-b + Math.sqrt(D)) / (2 * a);
            double root2 = (-b - Math.sqrt(D)) / (2 * a);
            System.out.println("Two distinct real roots: " + root1 + " and " + root2);
        } else if (D == 0) {
            double root = -b / (2 * a);
            System.out.println("Two equal real roots: " + root);
        } else {
            double realPart = -b / (2 * a);
            double imaginaryPart = Math.sqrt(-D) / (2 * a);
            System.out.println("Complex roots: " + realPart + " + " + imaginaryPart + "i and " + realPart + " - " + imaginaryPart + "i");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Example Run

Input:

Enter value of a: 1
Enter value of b: -3
Enter value of c: 2

 

Output:

Two distinct real roots: 2.0 and 1.0

Time and Space Complexity

Time Complexity

  • Calculating the discriminant (b² - 4ac) takes O(1).
     
  • Finding the square root takes O(1).
     
  • Performing basic arithmetic operations takes O(1).
     
  • Overall time complexity: O(1) (constant time).

Space Complexity

  • The program only uses a few extra variables (a, b, c, D, root1, root2, etc.) which take constant space.
     
  • Overall space complexity: O(1) (constant space).

Frequently Asked Questions

What happens if the coefficient ‘a’ is zero?

If a = 0, the equation becomes linear, and the quadratic formula cannot be used. In such cases, the equation can be solved as a simple linear equation bx + c = 0.

Can a quadratic equation have only one root?

Yes, when the discriminant D = 0, both roots are the same, meaning the equation has one unique real root.

What is the significance of the discriminant in a quadratic equation?

The discriminant D determines the nature of the roots: D > 0: Two real and distinct roots, D = 0: One real root (repeated) and D < 0: No real roots (complex roots exist).

Conclusion

In this article, we discussed how to solve quadratic equations in Java using the quadratic formula. We implemented logic to handle real and complex roots based on the discriminant value. This program is useful for mathematical computations, engineering applications, and algebraic problem-solving.

Live masterclass