Dark Mode On/Off

# Java Programs To Find All Roots of a Quadritic Equation

An equation is said to be a Quadratic Equation if it is of the form ax2+bx+c=0 where a,b,c are real numbers and a is not equal to 0. The standard form of the equation and the formula to calculate the same is as follows: A quadratic equation has two roots and these two roots depend on the discriminant. In the above formula, sqrt((b*b)-(4*a*c)) is known as the discriminant.

• If the discriminant is positive, then the roots are real and unequal.
• If the discriminant is 0, then the roots are real and equal.
• If the discriminant is negative, then the roots are unequal and imaginary.

Here, we are given a quadratic equation and our task is to find the roots of that equation. For example,

Input: 1 -2 1

Output: 1 1

## Program 1: Find Quadratic Equation

In this program, roots of the quadratic equations are found out in the main method itself.

### Algorithm:

1. Start
2. Declare variables a,b,c.
3. Initialize the variables a,b,c.
4. Calculate the roots of the quadratic equation in the main method itself.
5. Check whether roots are possible or not using condition a==0.
6. Calculate the discriminant as Discriminant= sqrt((b*b)-(4*a*c)).
7. If discriminant.>0, then the roots are real and unequal.
8. Calculate the roots as ( -b+ discriminant ) /(2 *a) and ( -b - discriminant ) /(2 *a).
9. If discriminant=0, then the roots are real and equal.
10. Calculate the roots as (-b + Discriminant) /( 2*a).
11. If discriminant>0, then the roots are complex and different.
12. Calculate the roots as (-b/(2*a)) + iD and (-b/(2*a)) - iD.
13. Display the roots.
14. Stop

Below is the code for the same.

In this example, we will directly find the roots of the quadratic equations. Firstly, the coefficients of the quadratic equations are declared and initialized. And then we directly find the roots of the quadratic equation in the main method itself.

``````//Java Program to find the roots of the quadratic equation
import java.util.Scanner;
{

public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a..");
System.out.println("Enter the value of b..");
System.out.println("Enter the value of c..");

double d=(b*b)-(4*a*c);    //Find the determinant
double D= Math.sqrt(d);
double r=2*a;

//Check for Roots
if(D>0)
{
System.out.println("Roots are real and unequal");
double root1=(D-b)/r;
double root2=(-D-b)/r;
System.out.println("Roots are..");
System.out.println(root1);
System.out.println(root2);
}
else if(D==0)
{
System.out.println("The roots of the quadratic equation are real and equal.");
double root=(-b)/r;
System.out.println("Root is "+root);
}
else
{
System.out.println("The roots of the quadratic equation are complex and different");
System.out.println("Roots are ");
System.out.println((-b/r)+" +i" + D);
System.out.println((-b/r)+" -i" + D);
}
}
}``````

Enter the value of a.. 15
Enter the value of b.. 68
Enter the value of c.. 3
Roots are real and unequal
Roots are..
-0.044555558333472335
-4.488777774999861

## Program 2: Find Quadratic Equation

In this method, roots of the quadratic equations are found out using functions.

### Algorithm

1. Start
2. Declare variables a,b,c.
3. Initialize the variables a,b,c.
4. Call a function to calculate the roots of the quadratic equation.
5. Check whether roots are possible or not using condition a==0.
6. Calculate the discriminant as Discriminant= sqrt((b*b)-(4*a*c))
7. If discriminant.>0, then the roots are real and unequal.
8. Calculate the roots as ( -b+ discriminant ) /(2 *a) and ( -b - discriminant ) /(2 *a).
9. If discriminant=0, then the roots are real and equal.
10. Calculate the roots as (-b + Discriminant) /( 2*a)
11. If discriminant>0, then the roots are complex and different.
12. Calculate the roots as (-b/(2*a)) + iD and (-b/(2*a)) - iD
13. Display the roots.
14. Stop

Below is the code for the same.

In this example, we will use methods to find the roots of the quadratic equations. Firstly, the coefficients of the quadratic equations are declared and initialized. And then a function is called to find the roots of the quadratic equation.

``````//Java Program to find the roots of quadratic equation using Functions
import java.util.Scanner;
import static java.lang.Math.*;

{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a..");
System.out.println("Enter the value of b..");
System.out.println("Enter the value of c..");

}

static void quadraticRoots(int a,int b,int c)
{
//Check whether roots are possible or not
if (a == 0)
{
System.out.println("The value of a cannot be 0.");
return;
}
//calculating discriminant (d)
int d = b * b - 4 * a * c;
double D = sqrt(abs(d));
if (d > 0)
{
System.out.println("The roots of the equation are real and different. \n");
System.out.println((double)(-b + D) / (2 * a) + "\n"+ (double)(-b - D) / (2 * a));
}
else if (d == 0)
{
System.out.println("The roots of the equation are real and same. \n");
System.out.println(-(double)b / (2 * a) + "\n"+ -(double)b / (2 * a));
}
// executes if d < 0
else
{
System.out.println("The roots of the equation are complex and different. \n");
System.out.println(-(double)b / (2 * a) + " + i"+ D + "\n"+ -(double)b / (2 * a)+ " - i" + D);
}
}
}``````

Enter the value of a.. 7
Enter the value of b.. 7
Enter the value of c.. 7
The roots of the equation are complex and different.

-0.5 + i12.12435565298214
-0.5 - i12.12435565298214