首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码块不打印特定格式

代码块不打印特定格式
EN

Stack Overflow用户
提问于 2016-10-04 16:04:48
回答 1查看 58关注 0票数 0
代码语言:javascript
复制
#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;
}

但是,我的输出是

代码语言:javascript
复制
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编写。

EN

回答 1

Stack Overflow用户

发布于 2016-10-04 16:14:18

试试这个:

代码语言:javascript
复制
#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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39847198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档