Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class prompts the user for the coefficients of their quadratic equation then calculates the roots of the equation.
- *
- * @author (Allie)
- * @version (4/26/17)
- */
- import java.util.Scanner;
- public class QuadraticEquation
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in); // importing the scanner
- 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.
- System.out.println();
- System.out.print("Please enter your first value a: "); // prompting the user for a
- double a = scan.nextDouble();
- System.out.print("Please enter your second value b: "); // prompting the user for b
- double b = scan.nextDouble();
- System.out.print("Please enter your third value c: "); // prompting the user for c
- double c = scan.nextDouble();
- double deter = (Math.pow(b,2)) - (4 * a * c); // calculating the determinant inside the radical
- if (deter > 0)
- {
- double num1 = ((-1)*b) + (Math.sqrt(deter)); // calculating the numerator of the equation for the first root
- double num2 = ((-1)*b) - (Math.sqrt(deter)); // calculating the numerator of the equation for the second root
- double root1 = num1 / (2*a); // calculating the first root
- double root2 = num2 / (2*a); // caclulating the second root
- System.out.println();
- System.out.println("The roots of your quadratic equation are " + root1 + " and " + root2 + "."); // printing the calculated roots
- System.out.println("Have a good day!");
- }
- else if (deter < 0)
- {
- System.out.println();
- System.out.println("Your quadratic equation does not have any real roots.");
- System.out.println("Have a good day!");
- }
- else
- {
- double num1 = ((-1)*b); // calculating the numerator of the only root when the determinant is 0
- double root = num1 / (2*a); //calculating the root
- System.out.println();
- System.out.println("The only root of your quadratic equation is " + root + "."); // printing the calculated root
- System.out.println("Have a good day!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement