利用c语言编写函数fun求1!+2!+3!+ …… +n!的和,在main函 数中由键盘输入n值,并输出运算结果。请编写fun 函数
时间: 2023-05-29 21:04:09 浏览: 859
```c
#include <stdio.h>
int fun(int n) {
int sum = 0, fac = 1;
for (int i = 1; i <= n; i++) {
fac *= i;
sum += fac;
}
return sum;
}
int main() {
int n;
printf("请输入n的值:");
scanf("%d", &n);
printf("1! + 2! + 3! + ... + %d! = %d\n", n, fun(n));
return 0;
}
```
相关问题
C语言编写函数fun求:1!+2!+3!+⋯⋯+n!的和的和
以下是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;
}
```
c语言编程求1!+2!+3!+……+n!
以下是C语言编程求1!+2!+3!+……+n!的函数fun以及在main函数中调用fun函数并输出结果的代码:
```c
#include <stdio.h>
int fun(int n) {
int i, j, sum = 0, temp = 1;
for (i = 1; i <= n; i++) {
temp = 1;
for (j = 1; j <= i; j++) {
temp *= j;
}
sum += temp;
}
return sum;
}
int main() {
int n, result;
printf("请输入n的值:");
scanf("%d", &n);
result = fun(n);
printf("%d的阶乘和为:%d\n", n, result);
return 0;
}
```
输入n的值后,程序会调用fun函数计算1!+2!+3!+……+n!的和,并输出结果。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)