ax^2 + bx + c = 0。假设吧^2-4ac<0,>用c语言
时间: 2024-06-01 10:09:30 浏览: 87
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Roots are: %.2f and %.2f", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2*a);
printf("Roots are: %.2f and %.2f", root1, root2);
}
else {
realPart = -b/(2*a);
imaginaryPart = sqrt(-discriminant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
阅读全文