Java Program to Find the Roots of a Quadratic Equation

This is the Java Program to Find the Roots of a Quadratic Equation.

Problem Description

Given the coefficients of the quadratic equation, write a Java Program to Find the roots of the quadratic equation.

Problem Solution

Calculate the determinant using the formula B2 – 4*A*C, and then according to its value calculate the roots, whether they are real and unequal, real and equal, or imaginary.

Program/Source Code

Here is the source code of the Java Program to Find the Roots of a Quadratic Equation. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Find the Roots of a Quadratic Equation
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Quadratic {
  8.     // Function to find and display the roots of the equation.
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         double a,b,c;
  12.         try{
  13.             System.out.println("Enter the coefficients of the quadratic equation");
  14.             a = Double.parseDouble(br.readLine());
  15.             b = Double.parseDouble(br.readLine());
  16.             c = Double.parseDouble(br.readLine());
  17.         }catch (Exception e){
  18.             System.out.println("An error occurred");
  19.             return;
  20.         }
  21.         double determinant = Math.pow(b,2) - 4*a*c;
  22.         if(determinant > 0){
  23.             System.out.println("Roots are " + (-b+Math.sqrt(determinant))/(2*a)
  24.                                   + " and " + (-b-Math.sqrt(determinant))/(2*a));
  25.         }else if (determinant == 0){
  26.             System.out.println("Roots are " + -b/(2*a));
  27.         }
  28.         else{
  29.             System.out.println("Roots are " + -b/(2*a) + "+i" + 
  30.                                 Math.sqrt(-determinant)/(2*a) + " and "
  31.                                 + -b/(2*a) + "-i" + Math.sqrt(-determinant)/(2*a));
  32.         }
  33.     }
  34. }
Program Explanation

In the function main(), firstly the coefficients are entered in the variables a,b and c respectively. Then the determinant is calculated (double determinant = Math.pow(b,2) – 4*a*c;). Now, according to the value of the determinant, the roots are displayed using an if-else ladder.

advertisement
advertisement

Time Complexity: O(1).

Runtime Test Cases
 
Case 1 (Simple Test Case - Real and Unequal Roots):
 
Enter the coefficients of the quadratic equation
1
-5
6
Roots are 3.0 and 2.0
 
Case 2 (Simple Test Case - Real and Equal Roots):
 
Enter the coefficients of the quadratic equation
1
-2
1
Roots are 1.0
 
Case 3 (Simple Test Case - Imaginary roots):
 
Enter the coefficients of the quadratic equation
1
1
1
Roots are -0.5+i0.8660254037844386 and -0.5-i0.8660254037844386

Sanfoundry Global Education & Learning Series – Java Programs..

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.