Java Program to Find all Roots of a Quadratic Equation
Java If else statement
- The if-else statement in Java is a conditional statement that is used to execute different blocks of code based on whether a certain condition is true or false.
- The if…else statement can improve code readability by allowing you to clearly express your intent and provide an organized structure for decision-making in your code.
- You must be familiar with following topics to understand the correspond example Such as: Java If else statement and Java Math sqrt().
- To understand the How to Find all Roots of a Quadratic Equation in Java , Read the Complete Article.
Steps to Find all Roots of a Quadratic Equation in java.
- Here are the Steps to Find all Roots of a Quadratic Equation in java .
- Define the coefficients of the quadratic equation. Let’s assume that the coefficients are represented by the variables a, b, and c.
- Calculate the discriminant, which is given by the formula: b^2 – 4ac. This will determine the nature of the roots.
- If the discriminant is positive, there are two real roots. Use the quadratic formula to calculate the roots, which are given by the formulas: (-b + sqrt(discriminant)) / (2a) and (-b – sqrt(discriminant)) / (2a).
- If the discriminant is zero, there is one real root. Use the formula: -b / (2a) to calculate the root.
- If the discriminant is negative, there are two complex roots. Use the formula: (-b ± sqrt(-discriminant)) / (2a) to calculate the roots.
Java Math sqrt():
The sqrt() method is a built-in method in the java.lang.Math class in Java. It is used to calculate and return the square root of a given number as a double value.
It's important to note that the sqrt() method will throw an exception of type java.lang.IllegalArgumentException if the argument passed to it is negative.
Here, num is the number whose square root needs to be calculated. The sqrt() method returns the square root of num as a double value.
The basic syntax of Java Math sqrt() is:
public static double sqrt(double num)
Example of Java Math sqrt() is:
double x = 25; double sqrtOfX = Math.sqrt(x); System.out.println("The square root of " + x + " is " + sqrtOfX);
In this example, the sqrt() method is used to calculate the square root of the number 25 and store it in the variable sqrtOfX. The result is then printed to the console.
Java If else statement:
The if...else statement in Java is used to execute a block of code if a certain condition is met, and a different block of code if the condition is not meet.
The condition can be any expression that evaluates to a boolean value (true or false). If the condition is true, then the code inside the first block (the "if" block) is executed.
If the condition is false, then the code inside the second block (the "else" block) is executed.
The basic syntax of Java if…else statement is
if (condition) { // Code to be executed if the condition is true } else { // Code to be executed if the condition is false }
Let’s look at the Java Program to Find Roots of a Quadratic Equation to perform certain operations.
Example 1: Java Program to Find Roots of a Quadratic Equation
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the coefficients a, b, and c: "); double a = sc.nextDouble(); double b = sc.nextDouble(); double c = sc.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("The equation has two real roots: " + root1 + " and " + root2); } else if (discriminant == 0) { double root = -b / (2 * a); System.out.println("The equation has one real root: " + root); } else { double realPart = -b / (2 * a); double imaginaryPart = Math.sqrt(-discriminant) / (2 * a); System.out.println("The equation has two complex roots: " + realPart + " + " + imaginaryPart + "i and " + realPart + " - " + imaginaryPart + "i"); } } }
Output
Enter the coefficients a, b, and c:
Explanation:
In this program, we first prompt the user to enter the values of a, b, and c.
Then, we calculate the discriminant using the formula b^2 - 4ac. Depending on the value of the discriminant, we compute the roots of the quadratic equation using the appropriate formula.
If the discriminant is positive, we compute two real roots using the quadratic formula (-b ± √(b^2 - 4ac)) / 2a. If the discriminant is zero, we compute a single real root using the formula -b / 2a.
If the discriminant is negative, we compute two complex roots using the formula (-b ± √(-1)(b^2 - 4ac)) / 2a.
Finally, we display the roots to the user using the System.out.println() method.
Example 2 :Java Program to Find all Roots of a Quadratic Equation
public class Main { public static void main(String[] args) { double a = 2.0; double b = 5.0; double c = -3.0; // Calculate the discriminant double discriminant = b * b - 4 * a * c; if (discriminant > 0) { // Two real roots double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); System.out.println("The roots are " + root1 + " and " + root2); } else if (discriminant == 0) { // One real root double root = -b / (2 * a); System.out.println("The root is " + root); } else { // Two complex roots double realPart = -b / (2 * a); double imaginaryPart = Math.sqrt(-discriminant) / (2 * a); System.out.println("The roots are " + realPart + " + " + imaginaryPart + "i and " + realPart + " - " + imaginaryPart + "i"); } } }
Output
The roots are 0.5 and -3.0
Explanation:
In this example, we use the coefficients a = 2.0, b = 5.0, and c = -3.0 to represent the quadratic equation 2x^2 + 5x - 3 = 0.
We calculate the discriminant using the formula b^2 - 4ac, and then use the value of the discriminant to determine the type and number of roots of the quadratic equation.
In this case, the discriminant is 5^2 - 4 * 2 * (-3) = 49, which is positive. This means that the quadratic equation has two real roots. We use the quadratic formula (-b ± √(b^2 - 4ac)) / 2a to calculate the roots, and then display them to the user using the System.out.println() method.
Example 3: Java Program to Find all Roots of a Quadratic Equation.
public class Main { public static void main(String[] args) { // get the values of a, b, and c from the user System.out.println("Enter the values of a, b, and c:"); double a = 4.5; double b = 5.5; double c = 6.5; // calculate the determinant double determinant = b * b - 4 * a * c; // calculate the roots of the equation if (determinant > 0) { double root1 = (-b + Math.sqrt(determinant)) / (2 * a); double root2 = (-b - Math.sqrt(determinant)) / (2 * a); System.out.println("The roots of the equation are: " + root1 + " and " + root2); } else if (determinant == 0) { double root = -b / (2 * a); System.out.println("The root of the equation is: " + root); } else { double realPart = -b / (2 * a); double imaginaryPart = Math.sqrt(-determinant) / (2 * a); System.out.println("The roots of the equation are: " + realPart + " + " + imaginaryPart + "i and " + realPart + " - " + imaginaryPart + "i"); } } }
Output
Enter the values of a, b, and c: The roots of the equation are: -0.6111111111111112 + 1.034885333899842i and -0.6111111111111112 - 1.034885333899842i
Explanation:
The program uses an if...else statement to check whether the determinant is positive, zero, or negative, and calculates the roots accordingly.
If the determinant is positive, the program calculates the two roots and prints them.
If the determinant is zero, the program calculates the single root and prints it.
If the determinant is negative, the program calculates the complex roots and prints them.
Finally, the program closes the Scanner object.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment