Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FindRealRootsOfQuadratic {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. System.out.print("For a quadratic ax^2 + bx + c = 0,\n Enter a, b, c:")
  7. double a = input.nextDouble();
  8. double b = input.nextDouble();
  9. double c = input.nextDouble();
  10. double discriminant = b*b - 4*a*c
  11. if (discriminant < 0) {
  12. System.out.println("The equation has no real roots.")
  13. } else if (discriminant == 0) {
  14. double root = (-b)/(2*a);
  15. System.out.println("The root is " & root);
  16. } else {
  17. double root1 = (-b + Math.sqrt(discriminant))/(2*a);
  18. double root2 = (-b - Math.sqrt(discriminant))/(2*a);
  19. System.out.println("The roots are " & root1 & " and " & root2);
  20. }
  21. }
  22. }
Add Comment
Please, Sign In to add comment