问题描述:
计算1/2+1/4+···+1/n,n为键盘输入的正偶数
代码实现:
1 #include<stdio.h>
2 int main(){
3 int even; //键盘输入的偶数
4 double sum=0.0;
5 int i=2;
6 printf("请输入偶数n:");
7 scanf("%d",&even);
8 for(;i<=even; i=i+2) {
9 sum += 1.0/i;
10 }
11 printf("1/2+1/4+···+1/n = %.6f\n",sum);
12 return 0;
13 }
运行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/0b0be929b41525f3a918e502f0f6cb71.png)