c语言写输入一元二次方程的三个系数求方程的实根
时间: 2024-05-09 12:20:19 浏览: 78
```
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, delta, x1, x2;
printf("请输入一元二次方程的三个系数(a,b,c):");
scanf("%f%f%f", &a, &b, &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;
}
```
相关问题
c语言写一元二次方程的三个系数求方程的实根
```c
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float delta, x1, x2;
printf("请输入一元二次方程的三个系数a、b、c:\n");
scanf("%f%f%f", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("方程的实根为:x1=%.2f,x2=%.2f\n", x1, x2);
} else if (delta == 0) {
x1 = x2 = -b / (2 * a);
printf("方程的实根为:x1=x2=%.2f\n", x1);
} else {
printf("方程无实根!\n");
}
return 0;
}
```
示例输出:
```
请输入一元二次方程的三个系数a、b、c:
2 -3 1
方程的实根为:x1=1.00,x2=0.50
```
用c语言写一元二次方程的三个系数求方程实根
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, delta, x1, x2;
printf("请输入一元二次方程的三个系数:\n");
scanf("%f%f%f", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("方程的实根为:%.2f和%.2f\n", x1, x2);
} else if (delta == 0) {
x1 = -b / (2 * a);
printf("方程的实根为:%.2f\n", x1);
} else {
printf("方程无实根\n");
}
return 0;
}
```
阅读全文