Advertisement
apez1

Quadratic Equation Calculator

Sep 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.io.*;
  2. import static java.lang.System.*;
  3. import java.util.Scanner;
  4. import java.lang.Math;
  5.  
  6. class Proj{
  7.  
  8.      public static void main (String str[]) throws IOException {
  9.  
  10.          
  11.       double a;
  12.       double b;
  13.       double c;
  14.       String response = null;
  15.       Scanner scanner = new Scanner(System.in);
  16.      
  17.       System.out.println("Please enter a value for A");
  18.       a = scanner.nextInt();
  19.       System.out.println("Please enter a value for B");
  20.       b = scanner.nextInt();
  21.       System.out.println("Please enter a value for C");
  22.       c = scanner.nextInt();
  23.      
  24.       double db = Math.pow(b,2);                                // B^2
  25.       double eb = (4)*(a)*(c);                                  //discriminant
  26.      
  27.       double fb = Math.sqrt((db) - (eb));                       //inside sqrt
  28.      
  29.       double gb = (-b) + (fb);                                  // +
  30.       double hb = (-b) - (fb);                                  // -
  31.      
  32.       double x1 = gb / (2*a);                                   //x sol w addition
  33.       double x2 = hb / (2*a);                                   //x sol w subtraction
  34.            
  35.       if (Double.isNaN(fb)) {                                   //Checks for no real solutions
  36.             System.out.println("No Real Solutions");
  37.         }    
  38.      
  39.        if(fb == 0) {                                            //Checks for 1 real solution
  40.           System.out.println("One Real Solution");
  41.           System.out.println("x = " + x1);  
  42.          
  43.           System.out.println("Would you like to see a factored form?"); // Asks if you would like to see a factored form
  44.           response = scanner.next();
  45.              
  46.           if (response.equalsIgnoreCase("Yes")) {               //Checks for if "yes" was said
  47.           double x5 = (-x1);
  48.          
  49.           if(x5 > 0) {                                          // +                                   
  50.              System.out.println("(x + " + x5 + ")");
  51.               }
  52.          else {
  53.              System.out.println("(x " + x5 + ")");              //-
  54.          }
  55.       }
  56.           else {
  57.               System.out.println("Okay");
  58.           }
  59.        }
  60.      
  61.     else if(fb > 0) {                                           //Checks for 2 solutions
  62.    
  63.        
  64.         System.out.println("x = " + x1);                        //Displays  x1 value
  65.      System.out.println("x = " + x2);                           //Displays  x2 value
  66.      
  67.      System.out.println("Would you like to see a factored form?"); // Asks if you would like to see a factored form
  68.      response = scanner.next();
  69.      
  70.      if (response.equalsIgnoreCase("Yes")) {                    //Checks for if "yes" was said
  71.  
  72.          double x3 = (-x1);
  73.          double x4 = (-x2);
  74.          
  75.          System.out.println("Factored form: ");                 //Displays Factored Form
  76.          
  77.          if(x3 > 0) {                                           // +                                   
  78.              System.out.println("(x + " + x3 + ")");
  79.               }
  80.          else {
  81.              System.out.println("(x " + x3 + ")");
  82.          }
  83.  
  84.        
  85.          if(x4 > 0) {                                           // -
  86.              System.out.println("(x + " + x4 + ")");
  87.               }
  88.          else {
  89.              System.out.println("(x " + x4 + ")");
  90.          }
  91.      }
  92.      
  93.      else {                                                     // if something other than yes is said
  94.             System.out.print("Okay.");
  95.      }
  96.     }    
  97.  
  98.      }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement