请用单重循环和双重循环(嵌套)两种方式来求1!+2!+...+10!的和。
时间: 2024-02-05 20:05:00 浏览: 151
C语言实现求1+2+3+……+100的和,要求分别用while、do while、for循环实现。.txt
以下是两种方式来求1!+2!+...+10!的和:
1. 单重循环实现:
```java
int sum = 0;
int factorial = 1;
for (int i = 1; i <= 10; i++) {
factorial *= i;
sum += factorial;
}
System.out.println("1!+2!+...+10!的和为:" + sum);
```
2. 双重循环(嵌套)实现:
```java
int sum = 0;
for (int i = 1; i <= 10; i++) {
int factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
System.out.println("1!+2!+...+10!的和为:" + sum);
```
阅读全文