# Java Program to Find the Roots of Quadratic Equation:

### Discuss Formula of Quadratic Equation:

• The standard formula of Quadratic Equation: ax^2 + bx + c=0 or ax2 + bx + c = 0, where

a, b and c are real numbers.

• Discriminant or Determinant(D) = b^2 – 4*a*c, tells the nature of the roots
1. If D is greater than 0, the roots are real and different.
2. If D is equal to 0, the roots are real and equal.
3. If D is less than 0, the roots are complex and different or imaginary number.

Formula to Find The Roots:

• Root1= (-b + sqrt(D))/2*a
• Root2= (-b – sqrt(D))/2*a

Question: Write a Java program that finds the roots of quadratic equation and input is given user.

Solution: Method 1

 import java.util.Scanner; public class quadraticEquation{ public static void main(String[] args) { int a, b, c; double root1, root2, D; Scanner s = new Scanner(System.in); System.out.println(“The Given Quadratic Equation:ax^2 + bx + c”); System.out.print(“Enter a:”); a = s.nextInt(); System.out.print(“Enter b:”); b = s.nextInt(); System.out.print(“Enter c:”); c = s.nextInt(); System.out.println(“The Given Quadratic Equation:”+a+”x^2 + “+b+”x + “+c); D = b * b – 4 * a * c; if(D > 0) { System.out.println(“Roots are real and unequal”); root1 = ( – b + Math.sqrt(D))/(2*a); root2 = ( – b – Math.sqrt(D))/(2*a); System.out.println(“First root = “+root1); System.out.println(“Second root = “+root2); } else if(D == 0) { System.out.println(“Roots are real and equal”); root1 = (-b+Math.sqrt(D))/(2*a); System.out.println(“Root:”+root1); } else { System.out.println(“Roots are imaginary”); } } }

 Output of this program: The Given Quadratic Equation:ax^2 + bx + c Enter a:4 Enter b:8 Enter c:2 The Given Quadratic Equation:4x^2 + 8x + 2 Roots are real and unequal First root = -0.2928932188134524 Second root = -1.7071067811865475

Question: Write a Java program that finds the roots of the quadratic equation and input is given user.

Solution: method 2

 import java.io.*; class quadraticEquation { public static void main(String args[])throws IOException { double  root1,root2,D=0,a,b,c; InputStreamReader obj=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(obj); System.out.println(“Enter the values of a,b,c”); a=Double.parseDouble(br.readLine()); b=Double.parseDouble(br.readLine()); c=Double.parseDouble(br.readLine()); if(D==0) { System.out.println(“roots are real and equal “); root1=root2=-b/(2*a); System.out.println(“roots are “+root1+”,”+root2); } else if(D>0) { System.out.println(“roots are real and unequal”); root1=(-b+Math.sqrt(D))/(2*a); root2=(-b+Math.sqrt(D))/(2*a); System.out.println(“roots are “+root1+”,”+root2); } else { System.out.println(“roots are imaginary”); }    }   } e

 Output of this program:   Enter the values of a,b,c 8 5 2 roots are real and equal roots are -0.3125,-0.3125

In the above two programs, the coefficients a, b and c are set by the user. Then, the determinant(D) is calculated as b2 – 4ac. Based on the value of D, the roots are calculated. Notice we’ve used library function Math.sqrt() to calculate the square root of a number in above programs. The calculated roots (either real or imaginary) are printed on the screen.