Java Arithmetic Operator solve quadratic equations

Question

We would like to solve quadratic equations using Java.

ax2 + bx + c = 0 b2 - 4ac is called the discriminant of the quadratic equation.
  • If it is positive, the equation has two real roots.
  • If it is zero, the equation has one root.
  • If it is negative, the equation has no real roots.

Write a program that prompts the user to enter values for a, b, and c.

Display the result based on the discriminant.



import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    // Create a Scanner object
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter values for a, b and c.
    System.out.print("Enter a, b, c: ");
    double a = input.nextDouble();
    double b = input.nextDouble();
    double c = input.nextDouble();

    double discriminant = Math.pow(b, 2) - 4 * a * c;

    System.out.print("The equation has ");
    if (discriminant > 0) {
      double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
      double root2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);
      System.out.println("two roots " + root1 + " and " + root2);
    } else if (discriminant == 0) {
      double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
      System.out.println("one root " + root1);
    } else
      System.out.println("no real roots");
  }
}

To define method for the calculation:



import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a, b, c: ");
    double a = input.nextDouble();
    double b = input.nextDouble();
    double c = input.nextDouble();

    displayRoots(a, b, c);/*from w  ww  . j a v  a2  s  .c o m*/
  }

  private static void displayRoots(double a, double b, double c) {
    double d = discriminant(a, b, c);
    double r1 = 0.0;
    double r2 = 0.0;
    StringBuilder output = new StringBuilder("The equation has ");

    if (d > 0) {
      r1 = calculateRoot(a, b, d, true);
      r2 = calculateRoot(a, b, d, false);
      output.append("two roots " + r1 + " and " + r2);
    } else if (d < 0) {
      output.append("no real roots");
    } else {
      r1 = calculateRoot(a, b, d, true);
      output.append("one root " + r1);
    }

    System.out.println(output);
  }

  private static double calculateRoot(double a, double b, double disc,
    boolean isR1) {
    double root;
    if (isR1) {
      root = (-b + Math.sqrt(disc)) / (2 * a);
    } else {
      root = (-b - Math.sqrt(disc)) / (2 * a);
    }
    return root;
  }

  private static double discriminant(double a, double b, double c) {
    return (b * b) - (4 * a * c);
  }
}



PreviousNext

Related