计算1-1/2+1/3-1/4+…+1/99-1/100+…,直到最后一项的绝对值小于10-4为止.C语言编程
我分别尝试了两个,但输出结果不一样。不知道哪个错了。#include<stdio.h>intmain(void){inti;intflag=1;doublesum=0;f...
我分别尝试了两个,但输出结果不一样。不知道哪个错了。
#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);
} 展开
#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);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询