Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void solveQuadratic()
- {
- Scanner input = new Scanner(System.in);
- double aValue;
- double bValue;
- double cValue;
- double discriminateValue;
- double bComputedValue;
- double divisorValue;
- double positiveRoots;
- double negativeRoots;
- System.out.println("\nThis program computes the solution for a quadratic equation.");
- System.out.printf("Please enter the A value: ");
- aValue = input.nextDouble();
- System.out.printf("Please enter the B value: ");
- bValue = input.nextDouble();
- System.out.printf("Please enter the C value: ");
- cValue = input.nextDouble();
- discriminateValue = (bValue * bValue) - (4 * aValue * cValue);
- bComputedValue = -bValue;
- divisorValue = 2 * aValue;
- positiveRoots = (bComputedValue + Math.sqrt(discriminateValue)) / divisorValue;
- negativeRoots = (bComputedValue - Math.sqrt(discriminateValue)) / divisorValue;
- if (discriminateValue > 0)
- {
- System.out.println("This problem has 2 real solutions.");
- }
- else if (discriminateValue == 0)
- {
- System.out.println("This problem has 1 real solution.");
- }
- else if(discriminateValue < 0)
- {
- System.out.println("This problem has no real solutions.");
- }
- System.out.printf("The solution(s) to this equation are: %s and %s.\n", positiveRoots, negativeRoots);
- }
Add Comment
Please, Sign In to add comment