.使用函数编写程序求n!(n!=1*2*3*4*......*n)
时间: 2023-12-22 19:05:12 浏览: 129
计算 S=1!+2!+3!+...+N! C语言代码
```c
#include <stdio.h>
// 求n的阶乘
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n = 0, sum = 0;
printf("请输入n值:\n");
scanf("%d", &n);
for (int j = 1; j <= n; j++) {
sum += factorial(j);
}
printf("%d", sum);
return 0;
}
```
阅读全文