Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Math;
- public class QuadraticEquation {
- public static void main(String[] args) {
- double a, b, c, x1, x2;
- double delta;
- Scanner input = new Scanner(System.in);
- System.out.println("Enter operand a: ");
- a = input.nextFloat();
- System.out.println("Enter operand b: ");
- b = input.nextFloat();
- System.out.println("Enter operand c: ");
- c = input.nextFloat();
- if (a == 0) {
- if (b == 0) {
- if (c == 0) {
- System.out.println("The equation has unlimited real roots.");
- } else {
- System.out.println("The equation has no real roots. ");
- }
- } else {
- x1 = (-c) / b;
- System.out.println("The equation has one root x1 = x2 = " + x1 );
- }
- } else {
- delta = Math.pow(b, 2) - (4 * a * c);
- if (delta < 0) {
- System.out.println("The equation has no real roots");
- } else {
- if (delta == 0) {
- x1 = -b / (2 * a);
- System.out.println("The equation has one root x1 = x2 = " + x1 );
- } else {
- if (delta > 0) {
- x1 = (-b + Math.sqrt(delta)) / (2 * a);
- x2 = (-b - Math.sqrt(delta)) / (2 * a);
- System.out.println("The equation has two real roots: " );
- System.out.println("X1 = " + x1);
- System.out.println("X2 = " + x2);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment