计算分数序列前n项和c语言代码
时间: 2023-11-11 20:03:14 浏览: 158
C语言程序设计-求一分数序列21,32,53,85,138,2113…的前n项之和;说明:每一分数的分母是前两项的分母之
5星 · 资源好评率100%
下面是计算分数序列前n项和的C语言代码:
```c
#include <stdio.h>
int main() {
int n;
double sum = 2.0, a = 2.0, b = 1.0, c;
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
c = a + b;
sum += c / b;
b = a;
a = c;
}
printf("%.2f", sum);
return 0;
}
```
阅读全文