1!+2!+.....+20! 用while循环
时间: 2023-11-27 09:03:04 浏览: 79
以下是使用while循环计算1!+2!+...+20!的方法:
```python
n = 1
sum = 0
while n <= 20:
i = 1
jie = 1
while i <= n:
jie = jie * i
i = i + 1
sum = sum + jie
n = n + 1
print(sum)
```
输出结果为:2561327494111820313
相关问题
1!+2!+.....+20! 用while循环c语言
以下是C语言实现1!+2!+...+20!的代码,使用了while循环和递归函数:
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int i = 1;
long long sum = 0;
while (i <= 20) {
sum += factorial(i);
i++;
}
printf("1!+2!+...+20! = %lld\n", sum);
return 0;
}
```
输出结果为:1!+2!+...+20! = 2561327494111820313
--相关问题--:
1. 如何在Python中实现1!+2!+...+20!?
2. 如何在Java中实现1!+2!+...+20!
while循环写1!+2!+3!+......+n!
n = int(input("请输入一个正整数:"))
i = 1
result = 1
while i <= n:
j = 1
factorial = 1
while j <= i:
factorial *= j
j += 1
result *= factorial
i += 1
print("{}! = {}".format(i-1, factorial))
print("结果为:", result)
阅读全文