#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, root1, root2;
printf("Input the coefficient a => ");
scanf("%lf", &a);
printf("Input the coefficient b => ");
scanf("%lf", &b);
printf("Input the coefficient c => ");
scanf("%lf", &c);
/* Compute the roots. */
root1 = (- b + sqrt(b*b-4*a*c))/(2*a);
root2 = (- b - sqrt(b*b-4*a*c))/(2*a);
printf("The first root is %8.3f\n", root1);
printf("The second root is %8.3f\n", root2);
return 0;
}但是,我的输出是
Input the coefficient a => 232
Input the coefficient b => 23
Input the coefficient c => 2
The first root is nan
The second root is nan 我只是个初学者,是不是格式有误?使用代码块,用C编写。
发布于 2016-10-04 16:14:18
试试这个:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, root1, root2;
double temp;
printf("Input the coefficient a => ");
scanf("%lf", &a);
printf("Input the coefficient b => ");
scanf("%lf", &b);
printf("Input the coefficient c => ");
scanf("%lf", &c);
/* Compute the roots. */
temp = b*b-4*a*c;
if (temp >= 0) {
root1 = (- b + sqrt(temp))/(2*a);
root2 = (- b - sqrt(temp))/(2*a);
printf("The first root is %8.3f\n", root1);
printf("The second root is %8.3f\n", root2);
} else {
printf("There is no root!\n");
}
return 0;
}记住:加载数学库,就像这样:->
-> "fileName“
-lm
https://stackoverflow.com/questions/39847198
复制相似问题