C语言课后习题
这是一个求π近似值以及循环次数的程序,请教各路高手有什么问题,我运行不出来。#include<stdio.h>
#include<math.h>
int main()
{
int n = 1, sign = 1, m = 0;
double pi = 0.0, t=1.0;
while (fabs(t) >= 1e-6)
{
pi = pi + t;
n = n + 2;
sign = -sign;
t = sign / n;
m++;
}
pi = pi * 4;
printf("%f,%d", pi , m);
return 0;
}
答案是这个,你们参考下:
#include<stdio.h>
#include<math.h>
int main()
{
int sign = 1, count = 0;
double pi = 0.0, n = 1.0, term = 1.0;
while (fabs(term) >= 1e-6)
{
pi = pi + term;
n = n + 2;
sign = -sign;
term = sign / n;
count++;
}
pi = pi * 4;
printf("%10.6f,%d", pi, count);
return 0;
}
[此贴子已经被作者于2019-8-11 14:02编辑过]