Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. // A program to calculate two roots of a quadratic equation
  2. // Assume that a<>0 and that b^2 >= 4ac is true
  3.  
  4. import java.util.*;
  5.  
  6. public class Workspace {
  7. public static void main(String[] args) {
  8. // declaration of variables
  9. double a, b, c, root1, root2, sqrtDiscr;
  10. Scanner scanner;
  11. scanner = new Scanner(System.in);
  12.  
  13. // input a, b and c
  14. System.out.print("Enter a (may not be 0): ");
  15. a = scanner.nextDouble();
  16. System.out.print("Enter b: ");
  17. b = scanner.nextDouble();
  18. System.out.print("Enter c: ");
  19. c = scanner.nextDouble();
  20.  
  21. // compute the two roots
  22. sqrtDiscr = Math.sqrt(Math.pow(b,2) - 4 * a * c);
  23. root1 = (-b + sqrtDiscr) / (2 * a);
  24. root2 = (-b - sqrtDiscr) / (2 * a);
  25.  
  26. // output the two roots
  27. System.out.println();
  28. System.out.println("Two roots of the equation, " + a + "*x*x + " + b + "*x + " + c + " = 0, are:");
  29. System.out.printf("%.2f and %.2f.", root1, root2);
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement