用c语言编写程序求ax+bx+c=0方程的根,其中a,b,c从键盘输入。
时间: 2023-05-15 18:04:58 浏览: 180
以下是用 C 语言编写程序求 ax^2 + bx + c = 0 方程的根的代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2;
printf("请输入 a, b, c 的值:\n");
scanf("%lf %lf %lf", &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;
}
```
这个程序可以通过输入 a、b、c 的值来求解方程 ax^2 + bx + c = 0 的根。如果方程无实数根,则输出提示信息;如果有两个相等的实数根,则输出这个根;如果有两个不相等的实数根,则输出这两个根。
相关问题
C语言 已知一元二次方程ax2+bx+c=0,从键盘输入a,b,c三个值,计算并输出两个根的值,结果保留两位。(其中b2-4ac>=0,)
在C语言中,你可以使用数学公式来解决这个问题。当解一元二次方程 \( ax^2 + bx + c = 0 \) 时,根据求根公式,如果判别式 \( b^2 - 4ac \geq 0 \),方程有两个实数根,它们可以表示为:
\[ x_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a} \]
\[ x_2 = \frac{-b - \sqrt{b^2 - 4ac}}{2a} \]
下面是一个简单的C程序,它会获取用户输入的a、b、c值,并计算并打印这两个根(假设输入有效,即\( a \neq 0 \))。
```c
#include <stdio.h>
#include <math.h>
double calculateRoots(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
if (discriminant >= 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
return (root1, root2);
} else {
printf("方程无实数根!\n");
return (0, 0);
}
}
int main() {
double a, b, c;
printf("请输入a, b, c的值: ");
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("错误!在一元二次方程中,a不能为0.\n");
return 1;
}
double roots[2];
roots = calculateRoots(a, b, c);
printf("方程的两个根是: %.2lf 和 %.2lf\n", roots[0], roots[1]);
return 0;
}
```
c语言求方程ax²+bx+c=0的根,用3个函数分别求当:b²-4ac大于0,等于0,小于0时的根并输出结果。从主函数输入a,b,c的值
以下是C语言求解一元二次方程的程序,其中包括三个函数分别求当b²-4ac大于0,等于0,小于0时的根并输出结果。从主函数输入a,b,c的值:
```c
#include <stdio.h>
#include <math.h>
void roots_greater_than_zero(float a, float b, float c);
void roots_equal_to_zero(float a, float b, float c);
void roots_less_than_zero(float a, float b, float c);
int main() {
float a, b, c;
printf("Please enter the value of a, b, c:\n");
scanf("%f%f%f", &a, &b, &c);
float discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
roots_greater_than_zero(a, b, discriminant);
} else if (discriminant == 0) {
roots_equal_to_zero(a, b);
} else {
roots_less_than_zero(a, b, discriminant);
}
return 0;
}
void roots_greater_than_zero(float a, float b, float discriminant) {
float root1 = (-b + sqrt(discriminant)) / (2 * a);
float root2 = (-b - sqrt(discriminant)) / (2 * a); printf("The roots are real and different.\n");
printf("root1 = %.2f, root2 = %.2f\n", root1, root2);
}
void roots_equal_to_zero(float a, float b) {
float root = -b / (2 * a);
printf("The roots are real and equal.\n");
printf("root1 = root2 = %.2f\n", root);
}
void roots_less_than_zero(float a, float b, float discriminant) {
float realPart = -b / (2 * a);
float imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("The roots are complex and different.\n");
printf("root1 = %.2f + %.2fi, root2 = %.2f - %.2fi\n", realPart, imaginaryPart, realPart, imaginaryPart);
}
```
阅读全文
相关推荐














