Can anyone help me out with this program

import java.util.Scanner;

public class Quadratic
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter three coefficients for a quadratic equation: ");
    double a = sc.nextDouble();
    double b = sc.nextDouble();
    double c = sc.nextDouble();

    if (a == 0.0)
      System.out.println("Error: a must be non-zero!");
    else
    {

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

      // Find out the root type by calling the method.
      int rtype = determineRootType(discriminant);

      // Do the rest.
      double root1, root2;

      switch (

    {
        {

        case 1: // two real roots ;
        root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
        root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
        System.out.println("Two roots: " + root1 + " and " + root2);
        break;
      case 2: // single root
        root1 = -b / (2 * a);
        System.out.println("Single root: " + root1);
        break;
      case 3: // two complex roots; it could be 'default' too.
        System.out.println("Two complex roots.");
      }
    }
}
  public static int determineRootType(double value)
  {
    if (value > 0)
      return 1;
    else if (value == 0)
      return 2;
    else
      return 3;
  }

}

Recommended Answers

All 2 Replies

This is the question

The root of the quadratic equation ax^2 + bx + c = 0, a ? 0 are given by the following formula:

-b +/- ?b^2 - 4ac
2a

In this formula, the term b^2 - 4ac = 0, then the equation has a single (repeated) root. If b^2 - 4ac > 0, the equation has two real roots. If b^2 - 4ac < 0, the equation has two complex roots. Write a program that prompts the user to input the value of a (the coefficient of x^2), b (the coeeficient of x), and c (the constant term), and outputs the type of roots of the equation. Furthermore, if b^2 - 4ac >= 0, the program should output the roots of the quadratic equation

You didn't say what help you need!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.