Java阶乘程序
 
 
 
  Java阶乘程序n的阶乘是所有正降序整数的
 乘积。 
  n 的阶乘由n!表示。例如: 
 
 
  
   4! = 4*3*2*1 = 245! = 5*4*3*2*1 = 120
  
 
 
  
 在这里4!被称为" 4阶乘"。
 
 阶乘通常用于组合和置换(数学)。
 
 有用Java语言编写阶乘程序的方法有很多种。让我们看看用Java编写阶乘程序的2种方法。
 
使用循环的程式程序 
使用递归的辅助程序 
在Java中使用循环的阶乘程序
 
 让我们看看在Java中使用循环的阶乘程序。
 
 
  
   class FactorialExample{
     public static void main(String args[]){
         int i,fact=1;
         int number=5;
         for(i=1;i<å=number;i++){
             fact=fact*i;
         }
         System.out.println("Factorial of "+number+" is: "+fact);
     }
 }
  
 
 
  
 输出: 
 
 
在Java中使用递归的阶乘程序
 
 让我们看看在Java中使用递归的阶乘程序。
 
 
  
   class FactorialExample2{
     static int factorial(int n){
         if (n == 0) return 1;
         else  return(n * factorial(n-1));
     }
     public static void main(String args[]){
         int i,fact=1;
         int number=4;
         //It is the number to calculate factorial fact = factorial(number);
         System.out.println("Factorial of "+number+" is: "+fact);
     }
 }
  
 
 
  
 输出: