本文主要是介绍请补充例5. 7程序,分别统计当fabs(t)>= le- 6和fabs(t)> = le- 8时执行循环体的次数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
2.请补充例5. 7程序,分别统计当“fabs(t)>= le- 6”和“fabs(t)> = le- 8”时执行循环体的次数。
fabs(t)>= le- 6 ,示例代码
# include <stdio.h>
# include <math.h>
int main()
{
int sign = 1;
double pi = 0.0, term = 1.0;
int n = 0;
while (fabs(term) >= 1e-6)
{
n++;
term = 1.0 / (2 * n - 1)*sign;
pi += term;
sign = -sign;
}
pi *= 4;
printf("pi的近似值是%lf\n", pi);
printf("循环体循环了%d次\n", n);
return 0;
}
运行截图:
fabs(t)> = le- 8,示例代码
# include <stdio.h>
# include <math.h>
int main()
{
int sign = 1;
double pi = 0.0, term = 1.0;
int n = 0;
while (fabs(term) >= 1e-8)
{
n++;
term = 1.0 / (2 * n - 1)*sign;
pi += term;
sign = -sign;
}
pi *= 4;
printf("pi的近似值是%lf\n", pi);
printf("循环体循环了%d次\n", n);
return 0;
}
运行截图:
这篇关于请补充例5. 7程序,分别统计当fabs(t)>= le- 6和fabs(t)> = le- 8时执行循环体的次数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!