SNOWSD

导航

c语言学习路程——前n项求和

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int n;
 5     double dividend, divisor;
 6     double sum = 0.0;
 7     int i;
 8     double t;
 9 
10     scanf_s("%d", &n);
11 
12     dividend = 2;
13     divisor = 1;
14     for (i = 1; i <= n; i++) {
15         sum += dividend / divisor;
16         t = dividend;
17         dividend = dividend + divisor;
18         divisor = t;
19     }
20     printf("%f %f\n", dividend, divisor);
21     printf("%.2f\n", sum);
22     return 0;
23 }

 

posted on 2021-01-09 16:01  SNOWSD  阅读(701)  评论(0编辑  收藏  举报