C语言 编程要求 例如:输入:5,表示求:1+2!+3!+4!+5!,输出:153
时间: 2023-12-07 09:38:38 浏览: 138
以下是C语言编写函数fun求1!+2!+3!+ …… +n!的和的代码,其中fun函数用于计算阶乘和,main函数用于输入n值并输出结果:
```c
#include <stdio.h>
int fun(int n) {
int i, sum = 0, sum1 = 1;
for (i = 1; i <= n; i++) {
sum1 *= i; // 计算i的阶乘
sum += sum1; // 将i的阶乘加到阶乘总和的结果中
}
return sum;
}
int main() {
int n;
printf("请输入n的值:");
scanf("%d", &n);
printf("1!+2!+3!+...+%d!的和为:%d\n", n, fun(n));
return 0;
}
```
输入n值后,程序会调用fun函数计算1!+2!+3!+ …… +n!的和,并输出结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)