Nguythang

Solve quadratic equation

May 11th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3.  
  4. public class QuadraticEquation {
  5.     public static void main(String[] args) {
  6.         double a, b, c, x1, x2;
  7.         double delta;
  8.         Scanner input = new Scanner(System.in);
  9.         System.out.println("Enter operand a: ");
  10.         a = input.nextFloat();
  11.         System.out.println("Enter operand b: ");
  12.         b = input.nextFloat();
  13.         System.out.println("Enter operand c: ");
  14.         c = input.nextFloat();
  15.  
  16.         if (a == 0) {
  17.             if (b == 0) {
  18.                 if (c == 0) {
  19.                     System.out.println("The equation has unlimited real roots.");
  20.                 } else {
  21.                     System.out.println("The equation has no real roots. ");
  22.                 }
  23.             } else {
  24.                 x1 = (-c) / b;
  25.                 System.out.println("The equation has one root x1 = x2 = " + x1 );
  26.             }
  27.         } else {
  28.             delta = Math.pow(b, 2) - (4 * a * c);
  29.             if (delta < 0) {
  30.                 System.out.println("The equation has no real roots");
  31.             } else {
  32.                 if (delta == 0) {
  33.                     x1 = -b / (2 * a);
  34.                     System.out.println("The equation has one root x1 = x2 = " + x1 );
  35.                 } else {
  36.                     if (delta > 0) {
  37.                         x1 = (-b + Math.sqrt(delta)) / (2 * a);
  38.                         x2 = (-b - Math.sqrt(delta)) / (2 * a);
  39.                         System.out.println("The equation has two real roots: " );
  40.                         System.out.println("X1 = " + x1);
  41.                         System.out.println("X2 = " + x2);
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment