Guest User

Untitled

a guest
Nov 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. public static void solveQuadratic()
  2. {
  3. Scanner input = new Scanner(System.in);
  4.  
  5. double aValue;
  6. double bValue;
  7. double cValue;
  8. double discriminateValue;
  9. double bComputedValue;
  10. double divisorValue;
  11. double positiveRoots;
  12. double negativeRoots;
  13.  
  14. System.out.println("\nThis program computes the solution for a quadratic equation.");
  15.  
  16. System.out.printf("Please enter the A value: ");
  17. aValue = input.nextDouble();
  18.  
  19. System.out.printf("Please enter the B value: ");
  20. bValue = input.nextDouble();
  21.  
  22. System.out.printf("Please enter the C value: ");
  23. cValue = input.nextDouble();
  24.  
  25. discriminateValue = (bValue * bValue) - (4 * aValue * cValue);
  26. bComputedValue = -bValue;
  27. divisorValue = 2 * aValue;
  28.  
  29. positiveRoots = (bComputedValue + Math.sqrt(discriminateValue)) / divisorValue;
  30. negativeRoots = (bComputedValue - Math.sqrt(discriminateValue)) / divisorValue;
  31.  
  32. if (discriminateValue > 0)
  33. {
  34. System.out.println("This problem has 2 real solutions.");
  35. }
  36. else if (discriminateValue == 0)
  37. {
  38. System.out.println("This problem has 1 real solution.");
  39. }
  40. else if(discriminateValue < 0)
  41. {
  42. System.out.println("This problem has no real solutions.");
  43. }
  44.  
  45. System.out.printf("The solution(s) to this equation are: %s and %s.\n", positiveRoots, negativeRoots);
  46.  
  47. }
Add Comment
Please, Sign In to add comment