Quadratic formula java – Java Program to Find the Roots of a Quadratic Equation

Quadratic formula java: Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Program to Find the Roots of a Quadratic Equation

Quadratic formula in java: In this article we will learn different ways to find roots of a quadratic equation in Java.

The standard form of a quadratic equation is

ax2 + bx + c = 0

where a, b, c are real numbers and a !=0.

We can find roots of a equation using following formula

x = (-b ± √(b2-4ac)) / (2a).

Where the ± sign indicates it contains two roots.

Now the term, b^2-4ac is known as Determinant. Determinant specifies the nature of roots i.e.

1.If Determinant>0, roots are real and distinct, roots can be determined by formula-

root1 = (-b + √(b2-4ac)) / (2a)

root2 = (-b – √(b2-4ac)) / (2a)

2. If Determinant==0, roots are real and equal, root can be determined by the formula

root1=root2= -b/2a

3. If Determinant>0, roots are complex and distinct, roots can be determined by the formula-

root1 = (-b + i√(b2-4ac)) / (2a)

root2 = (-b – i√(b2-4ac)) / (2a)

Now our aim is to find all the roots of a quadratic equation. To implement this, we can use different approaches. Let’s see one by one.

Let’s see the approaches one by one.

Method-I:- Find Roots of a Quadratic Equation using If-Else

Quadratic equation java: We can calculate square roots of a number using library function Math.sqrt(). Here we will use format() method as it returns formatted string which can take multiple arguments, while println() method takes only single argument. In place of format() we can also write print(). Now, let’s find the roots of a equation using following approach.

Approach:

  • Decalre and initialize coefficient values of a, b and c.
  • Declare two variable say root1 and roo2 calculate roots of the equation.
  • Calculate value of determinant by the formula, b^2-4ac.
  • Using IF check determinant > 0, then use the formula,

                root1 = (-b + √(b2-4ac)) / (2a)
                root2 = (-b – √(b2-4ac)) / (2a)

  • Using ELSE IF block, check whether determinant == 0, then use the formula,

                 root1=root2= -b/2a

  • ELSE the determinant will be > 0, then use the formula,

                root1 = (-b + i√(b2-4ac)) / (2a)
                root2 = (-b – i√(b2-4ac)) / (2a)

Program:

public class RootsQuadEqn 
{

  public static void main(String[] args) 
{

    // declare and initialize of a, b, c
    double a = 8.7, b = 10.5, c = 6.6;
    // root1 and roo2 are the 2 roots possible
    double root1, root2;

    // determinant can be calculated by (b^2 - 4ac)
    double determ = b * b - 4 * a * c;

    // checks if determinant>0
    if (determ > 0) {

      // roots are real and distinct
      root1 = (-b + Math.sqrt(determ)) / (2 * a);
      root2 = (-b - Math.sqrt(determ)) / (2 * a);

      System.out.format("root1 is %.2f and root2 is %.2f", root1, root2);
    }

    // checks if determinant=0
    else if (determ == 0) {

      // roots are real and equal 
      root1 = root2 = -b / (2 * a);
      System.out.format("root1 and root2 = %.2f;", root1);
    }

    // checks if determinant<0
    else {

      // roots are complex and distinct
      double realno = -b / (2 * a);
      double imaginaryno = Math.sqrt(-determ) / (2 * a);
      System.out.format("root1 is %.2f+%.2fi", realno, imaginaryno);
      System.out.format("\nroot2 is %.2f-%.2fi", realno, imaginaryno);
    }
  }
}

Output:

root1 = -0.60+0.63i
root2 = -0.60-0.63i

Method-II:- Find Roots of a Quadratic Equation using Function

Quadratic equation class java: In this method we will see how we can find roots of a quadratic equation using function call. Let’s use the below approach to implement it.

Approach:

  • Declare and initialize 3 coefficient variables i.e. a, b & c.
  • Call a function say computeRoots and pass the values a, b & c as arguments.
  • Inside function, implement the following.
  • Declare two variable say root1 and roo2 calculate roots of the equation.
  • Calculate value of determinant by the formula, b^2-4ac.
  • Using IF check determinant > 0, then use the formula,

                root1 = (-b + √(b2-4ac)) / (2a)
                root2 = (-b – √(b2-4ac)) / (2a)

  • Using ELSE IF block, check whether determinant == 0, then use the formula,

               root1=root2= -b/2a

  • ELSE the determinant will be > 0, then use the formula,

                root1 = (-b + i√(b2-4ac)) / (2a)
                root2 = (-b – i√(b2-4ac)) / (2a

Program:

import static java.lang.Math.*;  
public class RootsQuadEqn  
{  
//define a static method computeRoots that compute Roots  
static void computeRoots(double a, double b, double c)  
{  
  
// to calculate determinant  
double deter = b * b - 4 * a * c;  
double sqrtofdet = sqrt(abs(deter));  
// checks if determinant>0
if (deter > 0)   
{  
System.out.println("Roots of the quadratic eqution is real and distinct \n");  
// two roots will be calculated as (-b - sqrt(b2-4ac)) / (2a)
System.out.println((double)(-b + sqrtofdet) / (2 * a) + "\n"+ (double)(-b - sqrtofdet) / (2 * a));  
}  
// checks if determinant=0
else if (deter == 0)   
{  
System.out.println("Roots of the quadratic equation is real and equal \n"); 
// root1 and root2 can be calculated as -b/2a
System.out.println(-(double)b / (2 * a) + "\n"+ -(double)b / (2 * a));  
}  
// otherwise it checks if determinant>0
else   
{  
System.out.println("Roots of the quadratic equation is real and equal \n"); 
// two roots will be calculated as (-b + i*sqrt(b2-4ac)) / (2a)
System.out.println(-(double)b / (2 * a) + " + i"+ sqrtofdet + "\n"+ -(double)b / (2 * a)+ " - i" + sqrtofdet);  
}  
}  

public static void main(String args[])  
{  
double a = 1.5, b = 5, c = 2.7;      
//calling computeRoots function  
computeRoots(a, b, c);  
}  
}  

Output

Roots of the quadratic eqution is real and distinct

-0.6778402017205784

-2.655493131612755