Codex

Quadratic Roots

Jul 1st, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. /* This program solves the quadratic equations(ax^2+bx+c)
  2. and finds th values of x when user provides th value of a,b and c*/
  3. import java.io.*;
  4. class Quadratic
  5. {
  6.  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  7.  double A,B,C,R1,R2,D;
  8.  public void calcRoots() throws IOException
  9.  {  
  10.      System.out.println("Enter value of A");
  11.      A=Double.parseDouble(br.readLine());
  12.      System.out.println("Enter value of B");
  13.      B=Double.parseDouble(br.readLine());
  14.      System.out.println("Enter value of C");
  15.      C=Double.parseDouble(br.readLine());
  16.      D=(B*B)-(4*A*C);
  17.      if(D>=0)
  18.      {
  19.          R1=(-B+Math.sqrt(D))/(2*A);
  20.          R2=(-B-Math.sqrt(D))/(2*A);
  21.          System.out.println("Roots are = "+R1+"  "+R2);
  22.      }
  23.      else
  24.          System.out.println("Roots are imaginary");
  25.  }
  26.  public static void main(String args[])
  27. {
  28. Quadratic q=new Quadratic();
  29. q.calcRoots();
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment