一元二次方程求解个数
时间: 2024-06-12 10:10:13 浏览: 10
一元二次方程的求解个数取决于判别式的值。当判别式大于0时,方程有两个实数解;当判别式等于0时,方程只有一个实数解;当判别式小于0时,方程无实数解。根据引用和引用中的代码,可以使用判别式来判断方程的解的个数。需要注意的是,在编写if语句时,应该使用“==”来进行判断,而不是“=”,以避免出现错误。因此,可以使用以下代码来求解一元二次方程的解的个数:
```
if (d > 0) {
cout << "方程有两个实数解" << endl;
} else if (d == 0) {
cout << "方程只有一个实数解" << endl;
} else {
cout << "方程无实数解" << endl;
}
```
相关问题
c语言写一个一元二次方程求解
的程序,可以给出方程的根。以下是示例代码:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, discriminant, root1, root2;
printf("请输入一元二次方程的系数:\n");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("方程的两个实根为:%.2lf 和 %.2lf\n", root1, root2);
}
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("方程的唯一实根为:%.2lf\n", root1);
}
else
{
printf("方程没有实根!\n");
}
return 0;
}
C语言编写一个一元二次方程求解
的程序,可以给出实数解和虚数解。以下是代码:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, delta, x1, x2, realPart, imagPart;
printf("请输入一元二次方程的系数:\n");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
delta = b * b - 4 * a * c;
if (delta > 0)
{
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("方程有两个实数解:x1 = %lf, x2 = %lf\n", x1, x2);
}
else if (delta == 0)
{
x1 = x2 = -b / (2 * a);
printf("方程有一个实数解:x1 = x2 = %lf\n", x1);
}
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-delta) / (2 * a);
printf("方程有两个共轭复数解:x1 = %lf + %lfi, x2 = %lf - %lfi\n", realPart, imagPart, realPart, imagPart);
}
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)