C语言编写函数fun求:1!+2!+3!+⋯⋯+n!的和的和
时间: 2024-01-17 15:05:03 浏览: 150
C语言编程-编写函数fun求1!+2!+3!+ …… +n!的和,在main函数中由键盘输入n值,并输出运算结果;
5星 · 资源好评率100%
以下是C语言编写函数fun求1!+2!+3!+⋯⋯+n!的和的例子:
```c
#include <stdio.h>
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
int sumOfFactorials(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
return sum;
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int result = sumOfFactorials(n);
printf("The sum of factorials from 1 to %d is: %d\n", n, result);
return 0;
}
```
阅读全文