编写c语言程序,求一元二次方程ax²+bx+c=0的根
时间: 2024-05-25 15:17:36 浏览: 71
c代码-求ax平方+bx+c+=0
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, delta, x1, x2;
printf("请输入一元二次方程的系数:\n");
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);
delta = b * b - 4 * a * c;
if (delta < 0) {
printf("方程无实数解。\n");
} else if (delta == 0) {
x1 = x2 = -b / (2 * a);
printf("方程有两个相等的实数根:x1 = x2 = %.2f\n", x1);
} else {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("方程有两个不相等的实数根:x1 = %.2f,x2 = %.2f\n", x1, x2);
}
return 0;
}
阅读全文