Quadratic Equation Program in Java

In this post, we will develop a Java program for the quadratic equation. It will find the roots of the given quadratic equation.

A quadratic equation is an equation of the second degree, meaning it contains at least one term that is squared. The standard form of the quadratic equation is ax² + bx + c = 0 where a, b and c are real and a !=0, x is an unknown variable. The nature of roots is determined by the discriminant.

Quadratic formula Java

The discriminant of the Quadratic equation is calculated as b²-4ac.

discriminant(d) = b² - 4*a*c

The nature of the roots are given as,

=> If discriminant>1 then the roots are real and different
=> If discriminant=0 then the roots are real and equal
=> discriminant<1 then the roots are complex and different

For the quadratic equation ax² + bx + c = 0, if we denote the discriminant as d, then their roots

If d>1 then the roots are real and different
root1 = (-b + √d)/2a
root2 = (-b – √d)/2a

If d=0 then both roots are -b/2a

If d<1 then roots are complex and different
root1 = -b/2a + i (√d/2a)
root2 = -b/2a – i (√d/2a)

Java program to find roots of Quadratic Equation

import java.util.Scanner;

public class QuadraticProgram {

   public static void main(String[] args) {

      // declare variables
      int a, b, c;
      int desc ;
      int root1, root2 ;
      int realPart, imaginaryPart;

      // create Scanner class object 
      // to read inputs
      Scanner scan = new Scanner(System.in);
      // read inputs
      System.out.print("Enter coefficients "
		+ "(a, b, and c values): ");
      a = scan.nextInt();
      b = scan.nextInt();
      c = scan.nextInt();

      // display Quadratic equation
      System.out.print("The quadratic equation: ");
      System.out.format("%d*x^2 + %d*x + %d = 0\n",
                  		a, b, c);

      // calculate discriminant
      desc = (b*b) - (4*a*c);

      // find roots
      if(desc > 1) {
         // both roots are real and different 
         root1=(-b+(int)Math.sqrt(desc))/2*a;
         root2=(-b-(int)Math.sqrt(desc))/2*a;
         // display roots
         System.out.println("Roots are = "+ 
	    		root1 + ", "+ root2);
      } 

      else if(desc == 0) {
         // both roots are real and equal 
         root1=(-b+(int)Math.sqrt(desc))/2*a;
         root2 = root1;
         // display roots
         System.out.println("Roots are = "+ 
	    		root1+ ", "+ root2);
      } 

      else {
         // roots are complex and different
         realPart = -b/(2*a);
         imaginaryPart=(int)Math.sqrt(desc)/(2*a);
 	 System.out.format("root1 = %d + i(%d)\n",
			realPart, imaginaryPart);
	 System.out.format("root2 = %d - i(%d)\n",
			realPart, imaginaryPart);
      }

      // close Scanner class object
      scan.close();
   }
}

The output for the different test cases are:-

Enter coefficients (a, b, and c values): 1 -1 -6
The quadratic equation: 1*x^2 + -1*x + -6 = 0
Roots are = 3, -2

Enter coefficients (a, b, and c values): 1 0 -25
The quadratic equation: 1*x^2 + 0*x + -25 = 0
Roots are = 5, -5

Enter coefficients (a, b, and c values): 1 -12 36
The quadratic equation: 1*x^2 + -12*x + 36 = 0
Roots are = 6, 6

Enter coefficients (a, b, and c values): 1 4 5
The quadratic equation: 1*x^2 + 4*x + 5 = 0
root1 = -2 + i(0)
root2 = -2 – i(0)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *