(2)1!+2!+3!+4!+……+n! n=20 (n为奇数)
时间: 2023-11-24 15:51:54 浏览: 87
以下是使用Python实现1!+2!+3!+4!+……+n!的代码,其中n=20(n为奇数):
```python
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
n = 20
sum = 0
for i in range(1, n+1, 2):
sum += factorial(i)
print("n=%d, s=%d" % (n, sum))
```
输出结果为:n=20, s=2561327494111820313
相关问题
1!+2!+3!+4!+……+n! n=20 (n为奇数) c语言
以下是使用C语言计算1!+2!+3!+4!+……+n!的程序:
```c
#include <stdio.h>
int factorial(int num) {
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
int main() {
int n;
int sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
sum += factorial(i);
}
}
printf("%d\n", sum);
return 0;
}
```
c语言1!+2!+3!+4!+……+n! n=20 (n为奇数)
以下是C语言计算1!+2!+3!+4!+……+n!的代码,其中n=20且为奇数:
```c
#include <stdio.h>
int main()
{
int n = 20;
int sum = 0;
int factorial = 1;
for (int i = 1; i <= n; i++)
{
factorial *= i;
if (i % 2 == 1)
{
sum += factorial;
}
}
printf("%d\n", sum);
return 0;
}
```
阅读全文