Write a Java program to solve quadratic equations (use if, else if and else). Test Data Input a: 1 Input b: 5 Input c: 1 Expected Output : The roots are -0.20871215252208009 and -4.7912878474779195 code example
Example: how to do quadratic formula in java
public class Main {
public static void main(String[] args) {
double a = 2.3, b = 4, c = 5.6;
double root1, root2;
double determinant = b * b - 4 * a * c;
if (determinant > 0) {
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
}
else if (determinant == 0) {
root1 = root2 = -b / (2 * a);
System.out.format("root1 = root2 = %.2f;", root1);
}
else {
double real = -b / (2 * a);
double imaginary = Math.sqrt(-determinant) / (2 * a);
System.out.format("root1 = %.2f+%.2fi", real, imaginary);
System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}