Java Program to Find all Roots of a Quadratic Equation

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.

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.

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

Run

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: 

Example 2 :Java Program to Find all Roots of a Quadratic Equation

Run

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

Example 3: Java Program to Find all Roots of a Quadratic Equation.

Run

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription