c预言实现编写函数求n!,主函数通过调用函数实现sum=1!+2!+…n!(n由键盘输入)
时间: 2023-10-05 18:08:30 浏览: 88
C语言编程-编写函数fun求1!+2!+3!+ …… +n!的和,在main函数中由键盘输入n值,并输出运算结果;
5星 · 资源好评率100%
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
printf("Sum is %d", sum);
return 0;
}
```
阅读全文