jav编写程序求1!+2!+...+10!。
时间: 2023-11-09 12:01:52 浏览: 155
Java求1+2!+3!+...+20!的和的代码
5星 · 资源好评率100%
以下是 Java 代码实现:
```
public class FactorialSum {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += factorial(i);
}
System.out.println("1!+2!+...+10! = " + sum);
}
public static int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
```
输出结果为:1!+2!+...+10! = 4037913
阅读全文