Find Roots of a Quadratic Equation in Java



In this article, we will learn to find the roots of a quadratic equation using Java. The roots of a quadratic equation are determined by the following formula:

$$x = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$

Here we will take input values for the coefficients ?, ?, and ? of the quadratic equation ? ? 2 + ? ? + ? = 0. The program will determine the nature of the roots based on the value of the determinant ( ? 2 ? 4 ? ? ). If the determinant is greater than zero, there will be two distinct roots; if it is equal to zero, there will be exactly one root.

Problem Statement

Write a Java program to find the roots of a quadratic equation.

Input

Enter the value of a ::
15
Enter the value of b ::
68
Enter the value of c ::
3

Output

Roots are :: -0.044555558333472335 and -4.488777774999861

Steps to calculate the roots of a quadratic equation

Following are the steps to calculate the roots of a quadratic equation ?

  • Start by importing the Scanner class from java.util package.
  • Define the class, which contains the main method and declare variables for the two roots, firstRoot and secondRoot, to store the results.
  • Instantiate a Scanner object to read values entered by the user and ask the user to input the coefficients ?, ?, and ? of the quadratic equation and store these values.
  • Calculate Determinant: Compute the determinant based on the coefficients, which helps to determine the nature of the roots.
  • Check Determinant: If the Determinant is Positive: Calculate and display two distinct real roots.
  • If the Determinant is Zero: Determine and display one real root, indicating that it is a repeated root.
  • If the Determinant is Negative: Calculate the real and imaginary parts to indicate the presence of complex roots, then display them accordingly.
  • Finally, close the scanner to free up resources.

Java program to find the roots of a quadratic equation

Below is an example to find the roots of a quadratic equation in Java programming ?

import java.util.Scanner;

public class RootsOfQuadraticEquation {
    public static void main(String args[]) {
        double secondRoot = 0, firstRoot = 0;
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the value of a ::");
        double a = sc.nextDouble();

        System.out.println("Enter the value of b ::");
        double b = sc.nextDouble();

        System.out.println("Enter the value of c ::");
        double c = sc.nextDouble();

        // Calculate the determinant
        double determinant = (b * b) - (4 * a * c);

        // Check the value of the determinant
        if (determinant > 0) {
            double sqrt = Math.sqrt(determinant);
            firstRoot = (-b + sqrt) / (2 * a);
            secondRoot = (-b - sqrt) / (2 * a);
            System.out.println("Roots are :: " + firstRoot + " and " + secondRoot);
        } else if (determinant == 0) {
            firstRoot = (-b) / (2 * a);
            System.out.println("Root is :: " + firstRoot);
        } else {
            // If the determinant is negative, calculate complex roots
            double realPart = -b / (2 * a);
            double imaginaryPart = Math.sqrt(-determinant) / (2 * a);
            System.out.println("Roots are complex: " + realPart + " + " + imaginaryPart + "i and " + realPart + " - " + imaginaryPart + "i");
        }

        // Close the scanner
        sc.close();
    }
}

Output

Enter the value of a ::
15
Enter the value of b ::
68
Enter the value of c ::
3
Roots are :: -0.044555558333472335 and -4.488777774999861

Code explanation

In the program, we will take user input for the coefficients ?, ?, and ? of the equation ??2+??+?=0. It then calculates the determinant using the formula ?2?4??. Based on the value of the determinant, the program determines whether the equation has two distinct roots, one root, or no real roots. If the determinant is greater than zero, the program calculates and displays two real roots using the quadratic formula. If the determinant equals zero, it computes and prints the single root. If the determinant is negative, it informs the user that there are no real roots.

Updated on: 2024-10-21T17:40:26+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements