编写程序实现求 1!+2!+3!+...+20!。
时间: 2023-05-20 19:05:47 浏览: 53
以下是一个 Python 代码示例,用于计算 1! 到 20! 的乘积:
```
result = 1
for i in range(1, 21):
result *= i
print(result)
```
输出结果为:
```
2432902008176640000
```
这是 1! 到 20! 的乘积。
相关问题
用Java编写程序实现求 1!+2!+3!+...+20!。
可以使用BigInteger类来计算20的阶乘,然后将所有阶乘相乘即可。以下是Java代码示例:
import java.math.BigInteger;
public class Factorial {
public static void main(String[] args) {
BigInteger result = BigInteger.ONE;
for (int i = 1; i <= 20; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
System.out.println(result);
}
}
输出结果为2432902008176640000。
Java编写程序实现求1!+2!+3+...+20!。
```java
public class Factorial {
public static void main(String[] args) {
long result = 1;
for (int i = 1; i <= 20; i++) {
result *= i;
System.out.println(i + "! = " + result);
}
}
}
```
输出结果为:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
```