ax^2 + bx + c = 0。假设吧b^2-4ac>0,>用c语言
时间: 2024-05-25 09:18:56 浏览: 95
c代码-求ax平方+bx+c+=0
5星 · 资源好评率100%
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, x1, x2, discriminant;
printf("Enter coefficients a, b, c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The roots are real and different.\n");
printf("x1 = %.2f and x2 = %.2f\n", x1, x2);
} else if (discriminant == 0) {
x1 = x2 = -b / (2 * a);
printf("The roots are real and equal.\n");
printf("x1 = x2 = %.2f\n", x1);
} else {
printf("The roots are complex.\n");
}
return 0;
}
阅读全文