设n为自然数n=1*2*3(n-1)*n称为n的阶乘,并且0=1。用java编写程序计算2!4!6!10将结果输出
时间: 2023-12-30 07:03:50 浏览: 75
计算n的阶乘的一个方法,可以用多种形式输出的
Sure, I can help you with that! Here's the Java code:
```
public class Main {
public static void main(String[] args) {
int[] nums = {2, 4, 6, 10};
for (int num : nums) {
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println(num + "! = " + fact);
}
}
}
```
This program will calculate the factorials of 2, 4, 6, and 10, and output the results. Let me know if you have any other questions!
阅读全文