Go to Top

Friday 4 November 2011

To find the solution of a quadratic equation

Question 67 : Write a program in Java to find the solution of a quadratic equation by taking the inputs of a,b,c from the user.

Java Program :

import java.io.*;
class quadratic
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,c;
     double d,e;
     System.out.println("The format of a quadratic equation is given as follows : ");
     System.out.println("\nax2 + bx + c = 0");
     System.out.println("\nEnter values for a,b,c as follows : ");
     System.out.print("\na = ");
     a=Integer.parseInt(br.readLine());
     System.out.print("\nb = ");
     b=Integer.parseInt(br.readLine());
     System.out.print("\nc = ");
     c=Integer.parseInt(br.readLine());
     d=((-1*b)+java.lang.Math.sqrt((b*b)-(4*a*c)))/(2*a);
     e=((-1*b)-java.lang.Math.sqrt((b*b)-(4*a*c)))/(2*a);
     System.out.print("\nEquation : "+a+"x2");
     if(b<0)
     System.out.print(b+"x");
     else
     System.out.print("+"+b+"x");
     if(c<0)
     System.out.print(c);
     else
     System.out.print("+"+c);
     System.out.print(" = 0");
     System.out.println("\n\nSolution : x = ( "+d+" , "+e+" )");
    }
}
   

No comments:

Post a Comment

ShareThis