c语言:用do while循环求和1/2+1/4+1/6+1/8+…+1/40
答案:4 悬赏:60 手机版
解决时间 2021-02-07 11:09
- 提问者网友:刺鸟
- 2021-02-07 02:05
c语言:用do while循环求和1/2+1/4+1/6+1/8+…+1/40
最佳答案
- 五星知识达人网友:煞尾
- 2021-02-07 03:13
int main()
{
int i = 4,j;
int flag = 1;
float sum = 0.5;
do
{
j = 1;
int result = 1;
do
{
result *= j++;
} while (j <= i);
sum += (-1 + flag * 2) * 1.0f / (float)result;
flag ^= 1; // 0^1 = 1, 1^1 = 0
i+=2;
} while (i<=10);
return 0;
}
{
int i = 4,j;
int flag = 1;
float sum = 0.5;
do
{
j = 1;
int result = 1;
do
{
result *= j++;
} while (j <= i);
sum += (-1 + flag * 2) * 1.0f / (float)result;
flag ^= 1; // 0^1 = 1, 1^1 = 0
i+=2;
} while (i<=10);
return 0;
}
全部回答
- 1楼网友:鸽屿
- 2021-02-07 04:29
int n=0,sum=0;
do{
n+=2;
sum+=1/n;
}while(n<=40);
- 2楼网友:长青诗
- 2021-02-07 04:17
#include <stdio.h>
int main(int argc,char *argv[]){
int n=0;
double s=0;
do{
s+=1.0/(n+=2);
}while(n<40);
printf("The result are %f\n",s);
return 0;
}
- 3楼网友:逃夭
- 2021-02-07 03:48
您好,完整的程序如下,希望采纳哦~
#include <stdio.h>
main()
{
int i;
double sum=0.0;
i=2;
do
{
sum+=1.0/i;
i+=2;
}while(i<=40);
printf("%lf",sum);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯