用c语言输出一元二次方程ax2+bx+c=0的根.
时间: 2023-05-29 09:03:14 浏览: 88
```c
#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) {
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,用空格分隔。
输出格式:如果方程有两个实根,输出“方程的两个实根为:x1=xx,x2=xx”,保留两位小数;如果方程有两个相等的实根,输出“方程有两个相等的实根:x1=x2=xx”,保留两位小数;如果方程无实根,输出“方程无实根”。
相关问题
用c语言、计算一元二次方程ax2+bx+c=0的根。
好的,以下是用C语言计算一元二次方程ax2+bx+c=0的根的代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, delta, x1, x2;
printf("请输入一元二次方程的系数a、b、c:\n");
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;
}
```
编写代码实现一元二次方程ax2+bx+c=0的根。c语言
实现如下:
```
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, delta, x1, x2;
printf("请输入一元二次方程的系数(顺序为ax^2+bx+c=0)\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;
}
```
首先通过 `scanf()` 函数获取输入的系数 `a`、`b` 和 `c` 的值,然后计算判别式 `delta = b * b - 4 * a * c`。再根据判别式的值进行分类讨论,输出不同的根的解法,即实数根、相等实数根或无实数根。其中,`sqrt()` 表示计算平方根。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)