Do you think IIT Guwahati certified course can help you in your career?
No
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.
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:
The highest power of x must be 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.
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:
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):
D > 0 → Two distinct real roots.
D = 0 → Two equal real roots.
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:
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
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
Microsoft SDE Roadmap: Use AI Tools to Succeed
by Pranav Malik
19 May, 2025
01:30 PM
Break into MAANG Data Analyst roles from Non-Tech Profession
by Abhishek Soni
20 May, 2025
01:30 PM
SDE LinkedIn & Naukri Hacks to Get More Recruiter Calls
by Shantanu Shubham
21 May, 2025
01:30 PM
Amazon Data Analyst: Advanced Excel & AI Interview Tips
by Megna Roy
22 May, 2025
01:30 PM
Microsoft SDE Roadmap: Use AI Tools to Succeed
by Pranav Malik
19 May, 2025
01:30 PM
Break into MAANG Data Analyst roles from Non-Tech Profession