Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class FindRealRootsOfQuadratic {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("For a quadratic ax^2 + bx + c = 0,\n Enter a, b, c:")
- double a = input.nextDouble();
- double b = input.nextDouble();
- double c = input.nextDouble();
- double discriminant = b*b - 4*a*c
- if (discriminant < 0) {
- System.out.println("The equation has no real roots.")
- } else if (discriminant == 0) {
- double root = (-b)/(2*a);
- System.out.println("The root is " & root);
- } else {
- double root1 = (-b + Math.sqrt(discriminant))/(2*a);
- double root2 = (-b - Math.sqrt(discriminant))/(2*a);
- System.out.println("The roots are " & root1 & " and " & root2);
- }
- }
- }
Add Comment
Please, Sign In to add comment