我分别尝试了两个,但输出结果不一样。不知道哪个错了。
#include
<stdio.h>
int
main(void)
{
int i;
int flag = 1;
double sum = 0;
for (i=1; i<=10001; ++i)
{
sum += (double)flag / i;
flag = -flag;
}
printf("%lf\n", sum);
return 0;
}
#
include<stdio.h>
void main()
{
int n;
double sum=0,term,sign=1;
for(n=1;n<=10000;n++)
{
term = sign/n;
sign *= -1;
sum += term;
}
printf("the sum is %lf",sum);
}