JAVA定义求阶乘的方法,并利用该方法求解2! + 5! + 9! + 13!
时间: 2023-06-03 10:03:10 浏览: 73
用Java编程实现求阶乘和
以下是Java定义求阶乘的方法并利用该方法求解2! 5! 9! 13!的代码:
```
public class Main {
public static void main(String[] args) {
int[] nums = {2, 5, 9, 13};
for (int num : nums) {
int result = factorial(num);
System.out.println(num + "! = " + result);
}
}
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}
```
运行结果为:
```
2! = 2
5! = 120
9! = 362880
13! = 6227020800
```
阅读全文