Write a Java program to find roots of quadratic equation in all cases

By | 14.03.2017

Java program to find roots of quadratic equation

Write a Java program to find roots of quadratic equation in all cases. 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

r = −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

  • Enter the Quadratic Equation as an input. Now we use the given formulas to calculate the roots of the equation.The java.lang.* package consists of Math.sqrt() method which helps to calculate the Square Root of the Discriminant.
  • Here is the source code of the Java Program to Find the Roots of Quadratic Equation. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.




  • This very simple program inputs the quadratic equation by inputting the coefficients of x2 ,x and constant term.
  • After that the value of discriminant is calculated by its formula Discriminant=b^2-4ac.
  • As we know that the roots of a Quadratic equation depends largely on the value of discriminant ,three cases occur :

If discriminant is greater than zero than two distinct and real roots are obtained , which are calculated and given by Dharacharya formula.

If discriminant is zero then it means that the equation is a Perfect Square and two equal roots are obtained.

If discriminant is less than zero then no real roots are obtained and thus it has no real solution and since no datatype of JAVA can hold imaginary value, no solution can be represented here.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments