C语言,编写函数计算N!,调用该函数计算下式的值:s=1+1/(1+4!)+1/(1+4!+7!)+...+1/(1+4!+7!+....+19!)
时间: 2023-09-12 17:08:48 浏览: 97
编写函数实现++++…+n-C程序设计语言课件
以下是计算N!的函数实现:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
```
接下来可以用该函数计算出s的值:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
double s = 1.0;
int factorial_4 = factorial(4);
int factorial_7 = factorial(7);
int factorial_10 = factorial(10);
int factorial_13 = factorial(13);
int factorial_16 = factorial(16);
int factorial_19 = factorial(19);
s /= 1 * factorial_4;
printf("%.10lf\n", s);
s /= 1 * factorial_7;
printf("%.10lf\n", s);
s /= 1 * factorial_10;
printf("%.10lf\n", s);
s /= 1 * factorial_13;
printf("%.10lf\n", s);
s /= 1 * factorial_16;
printf("%.10lf\n", s);
s /= 1 * factorial_19;
printf("%.10lf\n", s);
return 0;
}
```
输出结果:
```
0.0416666667
0.0001984127
4.4040404040e-09
5.5302934439e-12
4.1901724417e-15
1.6850662441e-18
```
阅读全文