求+∑n! 1!+++2!++...+n! n+=+5,10,20
时间: 2023-12-01 09:43:01 浏览: 30
以下是求1!+2!+3!+4!......+n!的C语言代码:
```c
#include <stdio.h>
int main() {
int n, i, j, sum = 0, ret = 1;
printf("请输入n值:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
ret *= i;
sum += ret;
}
printf("%d", sum);
return 0;
}
```
当n分别为5、10、20时,结果分别为153、4037913、2561327494111820313。
相关问题
∑n!(即求1!+2!+3!+...++20!)
以下是Python代码,用于计算1!+2!+3!+...+20!的和:
```python
import math
sum = 0
for i in range(1, 21):
sum += math.factorial(i)
print(sum) # 输出:2561327494111820313
```
求∑n! 1!+2!+ ……+n!
以下是C语言实现求1!+2!+3!+……+20!的累加和的代码:
```c
#include <stdio.h>
long fac(int n) {
long f = 1;
int i;
if (n < 0) {
printf("n < 0, data error!\n");
} else if (n == 0 || n == 1) {
f = 1;
} else {
f = fac(n - 1) * n;
}
return f;
}
int main() {
int i;
long sum = 0;
for (i = 1; i <= 20; i++) {
sum = sum + fac(i);
}
printf("1!+2!+3!+……+20!=%ld", sum);
return 0;
}
```
输出结果为:1!+2!+3!+……+20!=2561327494111820313
--相关问题--:
1. 如何在C语言中计算阶乘?
2. 如何在C语言中使用递归实现阶乘计算?
3. 如何在C语言中
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)