Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
Java Program for Quadratic Equation (Find Roots With 3 Ways)
In mathematics and science, quadratic equations play a crucial role. They are fundamental in various fields, from physics and engineering to finance and computer science. A quadratic equation is a second-degree polynomial equation that takes the form:
ax2 + bx + c = 0
Here, 'a,' 'b,' and 'c' are coefficients, and 'x' represents the variable we're trying to solve for. The solutions to this equation, often referred to as the "roots" or "zeroes" of the quadratic equation, are the values of 'x' that make the equation true.
In this tutorial, we will learn how to solve quadratic equations in Java and find their roots using the power of code.
Use of Quadratic Equations
Before understanding the Java program for quadratic equation, you must know the use cases of these equations:
-
Physics: Quadratic equations frequently appear in physics to describe motion, energy, and forces. For example, they are used to calculate the trajectory of a projectile, model the behavior of oscillating systems, and determine the position of objects under the influence of gravity.
-
Engineering: Engineers use quadratic equations when designing structures, analyzing electrical circuits, and solving mechanical problems. They help in predicting how materials will deform under stress and in optimizing designs for efficiency.
-
Finance: In finance, quadratic equations are employed to evaluate investment returns, analyze risks, and model financial scenarios. They help investors and analysts make informed decisions about investments and loans.
-
Computer Science: Quadratic equations have applications in computer graphics, game development, and numerical analysis. Algorithms for solving quadratic equations are fundamental tools in computational mathematics.
-
Real-world Problem Solving: Beyond specialized fields, quadratic equations are used to solve a wide range of real-world problems, from calculating the dimensions of a rectangle with a given perimeter to determining the roots of a polynomial equation in error analysis.
So, let’s write the Java program for quadratic equation using various ways to have a solid grasp of how to find the roots efficiently and accurately.
Concepts to Learn:
Java Program for Quadratic Equation
Here's a Java program to solve a quadratic equation using the quadratic formula or Sri Dharacharya Formula:
Code
import java.util.Scanner;
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Quadratic Equation Solver");
System.out.println("Enter the coefficients of the quadratic equation (a, b, c):");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
// Calculate the discriminant (the value inside the square root)
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
// Two real and distinct roots
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Two real and distinct roots:");
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
} else if (discriminant == 0) {
// One real root (repeated)
double root = -b / (2 * a);
System.out.println("One real root (repeated):");
System.out.println("Root: " + root);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
System.out.println("Complex roots:");
System.out.println("Root 1: " + realPart + " + " + imaginaryPart + "i");
System.out.println("Root 2: " + realPart + " - " + imaginaryPart + "i");
}
scanner.close();
}
}
Output
Quadratic Equation Solver
Enter the coefficients of the quadratic equation (a, b, c):
1 -3 2
Two real and distinct roots:
Root 1: 2.0
Root 2: 1.0
Explanation
-
The user is prompted to enter the coefficients of the quadratic equation (a, b, c) using Scanner.
-
The program calculates the discriminant using the formula discriminant = b^2 - 4ac.
It then checks the value of the discriminant:
-
If the discriminant is greater than 0, there are two real and distinct roots. The program calculates and displays them.
-
If the discriminant is 0, there is one real root (repeated). The program calculates and displays it.
-
If the discriminant is less than 0, there are complex roots. The program calculates and displays them in the form of real and imaginary parts.
Quadratic Equation Program in Java Using Factoring
Here's a Java program for quadratic equation using factoring:
Code
import java.util.Scanner;
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Quadratic Equation Solver");
System.out.println("Enter the coefficients of the quadratic equation (a, b, c):");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant >= 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
} else {
System.out.println("The equation has complex roots.");
}
scanner.close();
}
}
Output
Quadratic Equation Solver
Enter the coefficients of the quadratic equation (a, b, c):
3 -9 1
Root 1: 2.8844373104863457
Root 2: 0.11556268951365418
Explanation
The user is prompted to enter the coefficients of the quadratic equation (a, b, c) using Scanner.
The program calculates the discriminant using the formula discriminant = b^2 - 4ac.
It then checks the value of the discriminant:
-
If the discriminant is greater than or equal to 0, it implies that the quadratic equation can be factored, and there are real roots. The program calculates and displays them using the quadratic formula.
-
If the discriminant is less than 0, it implies that the equation has complex roots, and this method does not apply.
-
This program uses factoring indirectly by first calculating the discriminant to determine whether the roots are real or complex. If the discriminant is non-negative, the program proceeds to calculate and display the real roots. If the discriminant is negative, it reports that the equation has complex roots.
Find Roots of Quadratic Equation in Java by Completing the Square
Here's a quadratic equation program in Java which involves completing the square:
Code
import java.util.Scanner;
public class QuadraticEquationSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Quadratic Equation Solver");
System.out.println("Enter the coefficients of the quadratic equation (a, b, c):");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
// Calculate the discriminant
double discriminant = b * b - 4 * a * c;
if (a == 0) {
System.out.println("This is not a quadratic equation (a cannot be zero).");
} else if (discriminant < 0) {
System.out.println("The equation has complex roots.");
} else {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
}
scanner.close();
}
}
Output
Quadratic Equation Solver
Enter the coefficients of the quadratic equation (a, b, c):
2 -4 8
The equation has complex roots.
Explanation
The user is prompted to enter the coefficients of the quadratic equation (a, b, c) using Scanner.
The program first checks if 'a' is equal to 0. If 'a' is 0, it indicates that it's not a quadratic equation, as division by zero is not allowed.
Next, the program calculates the discriminant using the formula discriminant = b^2 - 4ac.
The program then checks the value of the discriminant:
-
If 'a' is not zero, and the discriminant is negative, it implies that the equation has complex roots, and this method does not apply.
-
If 'a' is not zero, and the discriminant is non-negative, the program calculates and displays the real roots using the quadratic formula.
-
This program first checks for invalid input (a=0) and then determines whether the equation has real or complex roots based on the discriminant. If the discriminant is non-negative, it calculates and displays the real roots. If it's negative, it reports that the equation has complex roots.
Learn Next: