求1+1/(1*2)+1/(2*3)+...1/(n*(n+1))
程序代码:
/* 时间:2017年6月30日16:49:25 功能:求1+1/(1*2)+1/(2*3)+...1/(n*(n+1)) */ # include <stdio.h> int main(void) { int n, i; double s = 0, sum = 0; printf("请输入n的值:"); scanf("%d", &n); for (i=1; i<=n; ++i) //i值初始值为0,当n为0时,循环不执行; { s = 1.0 / (i*(i+1)); sum = sum + s; //sum是用来存储1/(1*2)+1/(2*3)+...1/(n*(n+1)的值 } sum += 1; // 以循环内部sum的最终值+1,得出题中的解; printf("%lf ", sum); return 0; }