Find Roots of Quadratic Equation Java Program

By | May 18, 2016

Java Program To Find Roots of a Quadratic Equation

Learn How To Find Roots of a Quadratic Equation in Java Programming Language. This Java Program To Compute Roots of Quadratic Equation makes use of If – Else Block.

What is a Quadratic Equation?

It is a term used in Elementary Algebra. The Standard Form of a Quadratic Equation is ax2 + bx + c = 0, where a, b, c are constant values which cannot be changed and x is a variable entity.

Quadratic Equation Formula

h = −b ± √(b2 − 4ac) / 2a

Conditions For Discriminants

  1. If b2 − 4ac = 0, One Real Solution is possible
  2. If b2 − 4ac is Positive, Two Real Solutions are possible
  3. If b2 − 4ac is Negative, Two Complex Solutions are possible

The java.lang.* package consists of Math.sqrt() method which helps to calculate the Square Root of the Discriminant.

Also Read: Java Program To Draw A Circle using Graphics

Java Program To Find Roots of a Quadratic Equation

import java.util.*;
import java.lang.*;

class quadratic_equation
    public static void main(String args[])
        double x, y, z;
        double root1, root2;
        double discriminant, sqr;
        System.out.println("\nEnter The Values");
        Scanner sc = new Scanner(;
        x = sc.nextFloat();
        y = sc.nextFloat();
        z = sc.nextFloat();
        discriminant = y*y - 4*x*z;
        sqr = Math.sqrt(discriminant);
            System.out.println("\nRoots Are Imaginary\n");
            root1 = (-y + sqr) / (2*x);
            root2 = (-y - sqr) / (2*x);
            System.out.println("\nRoot 1 = " + root1 + "\n");
            System.out.println("\nRoot 2 = " + root2 + "\n");

Also Read: Java Code To Convert Numbers To Words



Find Roots of a Quadratic Equation in Java Programming Language

If you have any compilation errors or doubts in this Program to Find Roots of a Quadratic Equation in Java Programming Language, let us know about in the Comment Section below.

Let's Discuss