c语言编写子函数fun求一分数序列 2/1, 3/2, 5/3, 8/5, 13/8, 21/13…的前n 项之和
时间: 2023-12-13 17:32:47 浏览: 138
以下是C语言编写的子函数fun求一分数序列 2/1, 3/2, 5/3, 8/5, 13/8, 21/13…的前n项之和的代码:
```c
#include <stdio.h>
double fun(int n)
{
double sum = 0.0;
double a = 2.0, b = 1.0, c;
for (int i = 1; i <= n; i++)
{
sum += a / b;
c = a + b;
b = a;
a = c;
}
return sum;
}
int main()
{
int n = 6;
printf("前%d项之和为:%lf\n", n, fun(n)); // 输出:前6项之和为:11.592857
return 0;
}
```
阅读全文