Advertisement
afrymire

QuadraticEquation

Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1.  
  2. /**
  3.  * This class prompts the user for the coefficients of their quadratic equation then calculates the roots of the equation.
  4.  *
  5.  * @author (Allie)
  6.  * @version (4/26/17)
  7.  */
  8. import java.util.Scanner;
  9. public class QuadraticEquation
  10. {
  11.    public static void main(String[] args)
  12.     {
  13.         Scanner scan = new Scanner(System.in); // importing the scanner
  14.        
  15.         System.out.println("Your equation should be in the format ax^2 + bx + c.");// informing the user of the names of the values in their equation.
  16.         System.out.println();
  17.        
  18.         System.out.print("Please enter your first value a: "); // prompting the user for a
  19.         double a = scan.nextDouble();
  20.      
  21.         System.out.print("Please enter your second value b: "); // prompting the user for b
  22.         double b = scan.nextDouble();
  23.        
  24.         System.out.print("Please enter your third value c: "); // prompting the user for c
  25.         double c = scan.nextDouble();
  26.    
  27.        
  28.         double deter = (Math.pow(b,2)) - (4 * a * c); // calculating the determinant inside the radical
  29.    
  30.         if (deter > 0)
  31.         {
  32.             double num1 = ((-1)*b) + (Math.sqrt(deter)); // calculating the numerator of the equation for the first root
  33.             double num2 = ((-1)*b) - (Math.sqrt(deter)); // calculating the numerator of the equation for the second root
  34.        
  35.             double root1 = num1 / (2*a); // calculating the first root
  36.             double root2 = num2 / (2*a); // caclulating the second root
  37.            
  38.             System.out.println();
  39.             System.out.println("The roots of your quadratic equation are " + root1 + " and " + root2 + "."); // printing the calculated roots
  40.             System.out.println("Have a good day!");          
  41.         }
  42.         else if (deter < 0)
  43.         {
  44.             System.out.println();
  45.             System.out.println("Your quadratic equation does not have any real roots.");
  46.             System.out.println("Have a good day!");
  47.         }
  48.        
  49.         else  
  50.         {
  51.             double num1 = ((-1)*b); // calculating the numerator of the only root when the determinant is 0
  52.            
  53.             double root = num1 / (2*a); //calculating the root
  54.            
  55.             System.out.println();
  56.             System.out.println("The only root of your quadratic equation is " + root + "."); // printing the calculated root
  57.             System.out.println("Have a good day!");
  58.         }    
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement