Advertisement
MrDoyle

Week#2 Quadratic Calculator

Oct 27th, 2020
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner (System.in);
  7.         int a = 0;
  8.         int b = 0;
  9.         int c = 0;
  10.  
  11.         System.out.println("Let´s solve quadratic equations");
  12.         System.out.println("ax^2 + bx + c = 0");
  13.         System.out.println("Please input your integer a: ");
  14.  
  15.         if (input.hasNextInt() ){
  16.             a = input.nextInt();
  17.             if (a ==0) {
  18.                 System.out.println("you cannot use a=0 for this quadratic calculator, the program will now close.");
  19.                 System.exit(0);
  20.             }
  21.         } else{
  22.             System.out.println("You did not enter an integer, the program will now close.");
  23.             System.exit(0);
  24.         }
  25.  
  26.         System.out.println("Please input your integer b: ");
  27.         if (input.hasNextInt() ){
  28.             b = input.nextInt();
  29.         } else{
  30.             System.out.println("You did not enter an integer, the program will now close.");
  31.             System.exit(0);
  32.         }
  33.  
  34.         System.out.println("Please input your integer c: ");
  35.         if (input.hasNextInt() ){
  36.             c = input.nextInt();
  37.         } else{
  38.             System.out.println("You did not enter an integer, the program will now close.");
  39.             System.exit(0);
  40.         }
  41.  
  42.  
  43.         double rootCheck = (b*b) -(4*a*c);
  44.         double root1;
  45.         double root2;
  46.  
  47.         if (rootCheck < 0) {
  48.             System.out.println("Sorry your equation has no real roots");
  49.         }else if ( rootCheck == 0){
  50.             System.out.println("Your equation has 2 equal roots");
  51.             root1 = (-b + rootCheck)/(2*a);
  52.             System.out.println("Your roots are: " + root1 + " & " + root1);
  53.         }else {
  54.             System.out.println("Your equation has 2 real roots");
  55.             root1 = (-b + rootCheck)/(2*a);
  56.             root2 = (-b - rootCheck)/(2*a);
  57.             System.out.println("Your roots are: " + root1 + " & " + root2);
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement