练习2-14 求奇数分之一序列前N项和 (15 分)

练习2-14 求奇数分之一序列前N项和 (15 分)

本题要求编写程序,计算序列 1 + 1/3 + 1/5 + ... 的前N项之和。

输入格式:

输入在一行中给出一个正整数N。

输出格式:

在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后6位。题目保证计算结果不超过双精度范围。

输入样例:

23

输出样例:

sum = 2.549541




#include <stdio.h>
#include <stdlib.h>


/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int x=1;
double s=0,y;
scanf("%d",&x);

for(y=1;y<=2*x;y+=2){
    s+=1.0/y;
}
printf("sum = %.6f\n",s);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xxl-h/p/11111055.html