Q:

Java program to find the roots of a quadratic equation

belongs to collection: Java Basic Programs

0

Java program to find the roots of a quadratic equation

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will read the value of ab, and c, and calculate the roots of a quadratic equation.

Program/Source Code:

The source code to find the roots of a quadratic equation is given below. The given program is compiled and executed successfully.

// Java program to find the roots 
// of a quadratic equation

import java.util.Scanner;

public class Main {
  static void calculateRoot(double a, double b, double c) {
    double rootA = 0;
    double rootB = 0;

    double realp = 0;
    double imagp = 0;
    double disc = 0;

    if (a == 0 || b == 0 || c == 0) {
      System.out.printf("Error: Unable to determine roots\n");
      return;
    } else {
      disc = b * b - 4.0 * a * c;
      if (disc < 0) {
        System.out.printf("Imaginary Roots\n");
        realp = -b / (2.0 * a);
        imagp = Math.sqrt(Math.abs(disc)) / (2.0 * a);
        System.out.printf("Root1 = %f  +i %f\n", realp, imagp);
        System.out.printf("Root2 = %f  -i %f\n", realp, imagp);
      } else if (disc > 0) {
        System.out.printf("Roots are real and distinct \n");
        rootA = (-b + Math.sqrt(disc)) / (2.0 * a);
        rootB = (-b - Math.sqrt(disc)) / (2.0 * a);
        System.out.printf("Root1 = %f  \n", rootA);
        System.out.printf("Root2 = %f  \n", rootB);
      } else if (disc == 0) {
        System.out.printf("Roots are real and equal\n");
        rootA = -b / (2.0 * a);
        rootB = rootA;
        System.out.printf("Root1 = %f\n", rootA);
        System.out.printf("Root2 = %f\n", rootB);
      }
    }
  }

  public static void main(String[] args) {
    double a = 0;
    double b = 0;
    double c = 0;

    Scanner X = new Scanner(System.in);

    System.out.printf("Enter the values of a: ");
    a = X.nextDouble();

    System.out.printf("Enter the values of b: ");
    b = X.nextDouble();

    System.out.printf("Enter the values of c: ");
    c = X.nextDouble();

    calculateRoot(a, b, c);
  }
}

Output:

Enter the values of a: 2
Enter the values of b: 3
Enter the values of c: 5
Imaginary Roots
Root1 = -0.750000  +i 1.391941
Root2 = -0.750000  -i 1.391941

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contain two static methods calculateRoot() and main().

The calculateRoot() method is used to find the quadratic equation based on given values of abc.

The main() method is an entry point for the program. Here, we read values ab, c from the user using the Scanner class. Then we used the calculateRoot() method to find the quadratic equation and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find the (GCD) Greatest Common Div... >>
<< Java program to convert a given number of days int...