用Java程序编写实现求1!+2!+3!+..+20!
时间: 2024-02-06 13:02:07 浏览: 68
Java求1+2!+3!+...+20!的和的代码
5星 · 资源好评率100%
public class Factorial {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
System.out.println(i + "! = " + factorial(i));
}
}
public static long factorial(int n) {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
阅读全文