2

Java Code for Solving Quadratic Equation

This code is written on JCreator... It will calculate the value of x in any quadratic equation.

import java.util.Scanner; 

class Functions
{
   public static void main(String arg[])
    {
      Scanner number = new Scanner(System.in); 
      double a, b, c, x1, x2, sqr, psqr, deno; 

      System.out.println("Enter a: "); 
      a = number.nextDouble(); 

      System.out.println("Enter b: "); 
      b = number.nextDouble(); 

      System.out.println("Enter c: "); 
      c = number.nextDouble(); 

      sqr = b*b;
      psqr = sqr - (4*a*c); 
      deno = 2 * a; 

      x1 = -b + Math.sqrt(psqr); 
      x1 = x1/deno; 

      x2 = -b - Math.sqrt(psqr); 
      x2 = x1/deno; 

      System.out.println("x = " + x1 + " Or x = " + x2); 
   } 
}