📜  递归求一个数的阶乘的Java程序

📅  最后修改于: 2022-05-13 01:54:54.254000             🧑  作者: Mango

递归求一个数的阶乘的Java程序

数 n 的因数定义为所有正降整数的乘积,n 的因数用 n! 表示。可以使用以下递归公式计算阶乘,其中递归调用是对小于计算阶乘的数字的所有数字的重数,因为计算阶乘的公式如下:

n! = n * [(n-1)!] 
i.e factorial of n (n!) = n * (n-1) * ......* 3 * 2* 1

注意: 0 的因数是 1

插图:

Input      : 5!
Processing : 5*4*3*2*1
Output     : 120  
Input      : 6!
Processing : 6*5*4*3*2*1
Output     : 720 

示例 1:

Java
// Java Program to Find Factorial of a Number
// where N>=0 is currently N>1
 
// Importing input output classes
import java.io.*;
// importing utiltity classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // To calculate factorial
    static int factorial(int n)
    {
 
        // Handling base case
        // iIf value of n=1 or n=0, it returns 1
        if (n == 0 || n == 1)
            return 1;
 
        // Generic case
        // Otherwise we do n*(n-1)!
        return n * factorial(n - 1);
    }
 
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
 
        // Calling method 1 to compute factorial and
        // storing the result into a variable
        int ans = factorial(5);
 
        // Print and display the factorial of number
        // customly passed as an argument
        System.out.println("Factorial of 5 is :" + ans);
    }
}


Java
// Java Program to Find Factorial of a Number
// where N>=0 is currently N=1
 
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // To calculate factorial
    static int factorial(int n)
    {
 
        // Handling base case
        // If value of n=1 or n=0 we return 1
        if (n == 0 || n == 1)
            return 1;
 
        // Generic case computation math
        // Otherwise we do n*(n-1)!
        return n * factorial(n - 1);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling Method 1 and
        // storing the result into variable
        int ans1 = factorial(0);
        int ans2 = factorial(1);
 
        // Print and display the factorial of 0
        System.out.println("Factorial of 0 is : " + ans1);
 
        // Similarly, Print and display the factorial of 1
        System.out.println("Factorial of 1 is : " + ans2);
    }
}


输出

Factorial of 5 is :120

输出说明:

最初,从 main 方法调用 factorial(),并以 5 作为参数传递。由于 5 大于或等于 1,因此 5 乘以 factorial( ) 的结果,其中传递 4 (n -1)。因为它是从同一个方法调用的,所以它是一个递归调用。在每次递归调用中,参数 n 的值减 1,直到 n 小于 1。当 n 的值小于或等于 1 时,则不进行递归调用,最后返回 1。

示例 2:

Java

// Java Program to Find Factorial of a Number
// where N>=0 is currently N=1
 
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // To calculate factorial
    static int factorial(int n)
    {
 
        // Handling base case
        // If value of n=1 or n=0 we return 1
        if (n == 0 || n == 1)
            return 1;
 
        // Generic case computation math
        // Otherwise we do n*(n-1)!
        return n * factorial(n - 1);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Calling Method 1 and
        // storing the result into variable
        int ans1 = factorial(0);
        int ans2 = factorial(1);
 
        // Print and display the factorial of 0
        System.out.println("Factorial of 0 is : " + ans1);
 
        // Similarly, Print and display the factorial of 1
        System.out.println("Factorial of 1 is : " + ans2);
    }
}
输出
Factorial of 0 is : 1
Factorial of 1 is : 1

输出说明:

最初,factorial() 是从 main 方法中调用的,并以 6 作为参数传递。在每次递归调用中,参数 n 的值减 1,直到 n 小于 1。对于 1!传递的减少参数为 0!作为要计算的小型递归调用。如上所述,零的阶乘是 1。因此,返回的小答案是 1,稍后乘以 1 本身,因此结果 1 作为输出。因此,规定当 n 的值小于或等于 0 时,则不计算递归调用,因为我们将遇到不允许计算阶乘的负整数。