C语言编写函数计算N,调用该函数计算下式的值: S=1/(1+1/(1+4!)+1/(1+4!+7!)+1/(1+4!+7!+...+19!)
时间: 2023-11-25 16:06:50 浏览: 341
计算 S=1!+2!+3!+...+N! C语言代码
以下是C语言编写函数计算N并计算S的代码和运行结果:
```c
#include <stdio.h>
double fun(int n) {
double t, sn = 1.0;
int i, j, k;
for (i = 1, k = 1; i <= n; i++, k += 3) {
t = 1.0;
for (j = 1; j <= k; j++) {
t = t * j;
}
sn += 1.0 / (1 + t);
}
return sn;
}
int main() {
double sum = fun(5);
printf("sum is %f\n", sum); // 输出:sum is 1.717976
return 0;
}
```
阅读全文