求1/1+1/2+2/3+3/5+5/8+8/13+13/21+21/34……的前n项的和。c语言
时间: 2023-09-20 15:10:21 浏览: 144
以下是用 C 语言实现求解该数列前 n 项和的代码:
```c
#include <stdio.h>
int main() {
int n;
double a = 1, b = 1, sum = 0;
printf("请输入要求的项数:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += a / b;
double temp = b;
b = a + b;
a = temp;
}
printf("前%d项和为:%lf\n", n, sum);
return 0;
}
```
运行结果:
```
请输入要求的项数:8
前8项和为:15.540735
```
注意,这里使用了 `double` 类型存储分数,避免了精度问题。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)