public class Practice0514{
public static long factorial1(int n){//递归计算n的阶乘
if(n==0||n==1){
return 1;
}
else{
return factorial1(n-1)*n;
}
}
public static long factorial2(int n){//非递归计算n的阶乘
long ret=1;
int i=0;
for(i=1;i<=n;i++){
ret=ret*i;
}
return ret;
}
public static void main(String args[]){
int n=18;//n从这里指定
System.out.printf("%d的阶乘为%d%n",n,factorial1(n));
System.out.printf("%d的阶乘为%d%n",n,factorial2(n));
}
}
结果如图所示:
原文:https://blog.51cto.com/14234228/2396537