代码块不打印特定格式

时间:2016-10-04 08:04:48

标签: c format printf

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 

但是,我的输出是

label

我只是一个初学者,格式错了吗? 使用代码块,用C语言编写。

1 个答案:

答案 0 :(得分:0)

试试这个:

#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;
}
  

记住:像这样加载数学库 - &gt; gcc“fileName”-lm