Java Program to Find all Roots of a Quadratic Equation

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to calculate the roots of a quadratic equation in Java. A quadratic equation is an algebraic expression of the second degree or in other words, it has two results i.e. real number and an imaginary number.

Below is a demonstration of the same −

Given a quadratic equation of the form ax2 + bx + c −

There are three cases:
b2 < 4*a*c - The roots are not real i.e. they are complex
b2 = 4*a*c - The roots are real and both roots are the same.
b2 > 4*a*c - The roots are real and both roots are different

Input

Suppose our input is −

a = 1, b = 2, c = 3

Output

The desired output would be −

The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i

Algorithm

Step1- Start
Step 2- Declare 6 double values: a, b, c, root_1, root_2, quadratic_equation
Step 3- Prompt the user to enter a,b,c double values/ define the double values
Step 4- Read the values
Step 5- In a for loop, check if the value of quadratic_equation variable is greater than 0, and if
true, use quadric formula to find the value, and assign it to a variable.
Step 6- Display the result
Step 7- Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class QuadraticEquation {
   public static void main(String[] args) {
      double a, b, c, root_1, root_2, quadratic_equation;
      double real_number, imaginary_number;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A scanner object has been defined ");
      System.out.print("Enter the value of a : ");
      a = my_scanner.nextDouble();
      System.out.print("Enter the value of b : ");
      b = my_scanner.nextDouble();
      System.out.print("Enter the value of c : ");
      c = my_scanner.nextDouble();
      quadratic_equation = b*b - 4*a*c ;
      if (quadratic_equation > 0) {
         root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a);
         root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a);
         System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
      }
      else 
      if (quadratic_equation == 0) {
         root_1 = root_2 = -b / (2 * a);
         System.out.format("root_1 = root_2 = %.2f;", root_1);
      }
      else {
         real_number = -b / (2 * a);
         imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a);
         System.out.println("The roots of the quadratic equation are");
         System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number);
         System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number);
      }
   }
}

Output

Required packages have been imported
A scanner object has been defined
Enter the value of a : 1
Enter the value of b : 2
Enter the value of c : 3
The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class QuadraticEquation {
    public static void main(String[] args) {
      double a, b, c, root_1, root_2, quadratic_equation;
      double real_number, imaginary_number;
      a = 1;
      b = 2;
      c = 3;
      System.out.println("The three numbers are defined as " +a +", " +b +" and " +c);
      quadratic_equation = b*b - 4*a*c ;
      if (quadratic_equation > 0) {
         root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a);
         root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a);
         System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
      }
      else 
      if (quadratic_equation == 0) {
         root_1 = root_2 = -b / (2 * a);
         System.out.format("root_1 = root_2 = %.2f;", root_1);
      }
      else {
         real_number = -b / (2 * a);
         imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a);
         System.out.println("The roots of the quadratic equation are");
         System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number);
         System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number);
      }
   }
}

Output

The three numbers are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i
raja
Updated on 21-Feb-2022 11:23:44

Advertisements