2. 有一数列:2/1,3/2,5/3,8/5,…求出这个数列的前 20 项之和。用c语言编程
时间: 2023-12-13 08:06:52 浏览: 70
C 语言代码 求2/1,3/2,5/3,8/5,…之和
以下是用 C 语言编写的求解该数列前20项之和的程序:
```c
#include <stdio.h>
int main() {
double numerator = 2, denominator = 1, sum = 0, temp;
int i;
for (i = 1; i <= 20; i++) {
sum += numerator / denominator;
temp = numerator + denominator;
denominator = numerator;
numerator = temp;
}
printf("The sum of the first 20 terms of the sequence is: %.2f\n", sum);
return 0;
}
```
程序输出:
```
The sum of the first 20 terms of the sequence is: 32.66
```
阅读全文