一元二次方程的根c语言代码
时间: 2023-05-20 07:07:00 浏览: 206
以下是一元二次方程的根的 C 语言代码:
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Roots are real and different.\n");
printf("Root1 = %.2f and Root2 = %.2f", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2*a);
printf("Roots are real and same.\n");
printf("Root1 = Root2 = %.2f", root1);
}
else {
printf("Roots are complex and different.\n");
printf("Root1 = %.2f + %.2fi and Root2 = %.2f - %.2fi", -b/(2*a), sqrt(-discriminant)/(2*a), -b/(2*a), sqrt(-discriminant)/(2*a));
}
return 0;
}
希望这个代码对你有帮助!
阅读全文