Using the formula x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2 *a) Find the roots of the quadratic equation ax2+ bx + c = 0, where a is not equal to 0.
Input
Enter a line containing three floating point numbers a, b, c (separated by a space), which represent the coefficients of the equation ax2 + bx + c = 0, respectively.
Output
Output a line representing the solution to the equation.
If b2 = 4 * a * c, the two real roots are equal, and the output form is: x1=x2=….
If b2 > 4 * a * c, the two real roots are not equal, and the output form is: x1=…;x2 = …, where x1>x2.
If b2 < 4 * a * c, there are two imaginary roots, then output: x1 = real part + imaginary part i; x2 = real part - imaginary part i, that is, the imaginary part coefficient of x1 is greater than or equal to the imaginary part coefficient of x2 , which cannot be omitted when the real part is 0. real part = -b / (2*a), imaginary part = sqrt(4*a*cb*b) / (2*a)
All real numbers are required to be accurate to 5 decimal places, with no spaces between numbers and symbols.
Sample input
1.0 2.0 8.0
Sample output
x1=-1.00000+2.64575i;x2=-1.00000-2.64575i
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double a,b,c;
Scanner scan=new Scanner(System.in);
a=scan.nextDouble();
b=scan.nextDouble();
c=scan.nextDouble();
double x1 = (-b + Math.sqrt(b*b-4*a*c))/(2*a);
double x2 = (-b - Math.sqrt(b*b-4*a*c))/(2*a);
if(b*b==4*a*c) System.out.println("x1"+"="+"x2"+"="+String.format("%.5f",x1));
else if(b*b > 4*a*c)
{ System.out.print("x1"+"="+String.format("%.5f",x1)+";");
System.out.print("x2"+"="+String.format("%.5f",x2));
}
else
{
double shibu=0,xubu=0;
shibu=-b/(2*a);
xubu=Math.sqrt(4*a*c-b*b)/(2*a); System.out.print("x1"+"="+String.format("%.5f",shibu)+"+"+String.format("%.5f",xubu)+"i"+";"); System.out.print("x2"+"="+String.format("%.5f",shibu)+"-"+String.format("%.5f",xubu)+"i");
}
}
}