Quadratic equation program in Java
In this post, you will learn how to find the roots of a quadratic equation using the Java programming language.
A quadratic equation in math is a second-degree equation of the given form-
ax² + bx + c = 0
Here, the coefficients are a and b, the constant term is c, and the variable is x. Since the variable x is of the second degree, there are two roots or answer for this quadratic equation.
The universal rule of quadratic equation defines that the value of 'a' cannot be zero, and the value of x is used to find the roots of the quadratic equation (a, b).
Roots of quadratic equation
The discriminant of a quadratic equation is calculated as b2 - 4ac. The nature of the roots depends on the Discriminant (D),
- If D > 0, the roots are real and distinct (unequal)
- If D = 0, the roots are real and equal.
- If D < 0, the roots are real and imaginary.
The given table represents the nature of the quadratic equation roots-
If discriminant(D) > 0 | |
If discriminant = 0 | |
If discriminant < 0 |
Algorithm of Roots of quadratic equation
1. Start
2. Input the coefficient variable, a, b and c.
3. D <- sqrt (b * b - 4 * a * c).
4. R1 <- (-b + D) / ( 2 * a).
5. R2 <- (-b - D) / (2 * a).
6. Print the roots R1 and R2.
7. Stop
Java Program to Find Roots of a Quadratic Equation
import static java.lang.Math.*;
import java.util.Scanner;
public class QuadraticEquationProgram
{
static void QuadraticEquationRoots(float a, float b, float c)
{
if (a == 0)
{
System.out.println("The value of a cannot be 0.");
return;
}
//calculating discriminant (d)
float discriminant = b * b - 4 * a * c;
double sqrtval = sqrt(abs(discriminant));
if (discriminant > 0)
{
System.out.println("Roots are real and different:\n");
System.out.println((double)(-b + sqrtval) / (2 * a) + "\n"+ (double)(-b - sqrtval) / (2 * a));
}
else if (discriminant == 0)
{
System.out.println("Roots 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("Roots are complex:\n");
System.out.println(-(double)b / (2 * a) + " + i"+ sqrtval + "\n"+ -(double)b / (2 * a)+ " - i" + sqrtval);
}
}
//main method
public static void main(String args[])
{
float a, b,c;
Scanner scan = new Scanner(System.in);
// read inputs
System.out.print("Enter coefficients "
+ "(a, b, and c values): ");
a = scan.nextFloat();
b = scan.nextFloat();
c = scan.nextFloat();
//calling function
QuadraticEquationRoots(a, b, c);
}
}
Output of the above code:
Enter coefficients (a, b, and c values): 5.3 4.2 3.2
Roots are complex:
-0.3962263828412044 + i7.0851961566096895
-0.3962263828412044 - i7.0851961566096895
Enter coefficients (a, b, and c values): 4.2 6.3 2.3
Roots are real and different:
-0.6280121432410709
-0.8719879702915909
Enter coefficients (a, b, and c values): 2 4 5.2
Roots are complex:
-1.0 + i5.059644105480255
-1.0 - i5.059644105480255
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java