Shavit

Quadratic Equation V2

Oct 15th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class QuadraticEquationV2 {
  7.  
  8.         public static void main(String[] args)
  9.        
  10.         {
  11.                 Scanner in = new Scanner (System.in);
  12.                 int a, b, c;
  13.                
  14.                 System.out.println("Enter the values of a, b and c for the quadratic equation:");
  15.                 a = in.nextInt();
  16.                 b = in.nextInt();
  17.                 c = in.nextInt();
  18.                
  19.                 double delta = Math.pow((double)b, 2) - 4 * a * c;
  20.                 double x1 = (-b + Math.sqrt(delta)) / (2 * a);
  21.                 double x2 = (-b - Math.sqrt(delta)) / (2 * a);
  22.                
  23.                 if (delta < 0)
  24.                 {
  25.                     System.out.println("There are no roots for your equation!");
  26.                 }
  27.                 else if (delta == 0)
  28.                 {
  29.                     System.out.printf("There is one root for your equation, it is %.2f", x1);
  30.                 }
  31.                 else if (delta > 0)
  32.                 {
  33.                     System.out.printf("There are two roots for your equation, they are %.2f and %.2f", x1, x2);
  34.                 }
  35.                
  36.                 in.close();
  37.         }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment