Java Program to Find Factorial of a Number Recursively
Factorial of a number n is defined as a product of all positive descending integers, Factorial of n is denoted by n!. Factorial can be calculated using the following recursive formula where the recursive call is made to a multiplicity of all the numbers lesser than the number for which the factorial is computed as the formula to calculate factorial is as follows:
n! = n * [(n-1)!]
i.e factorial of n (n!) = n * (n-1) * ......* 3 * 2* 1
Note: Factorial of 0 is 1
Illustration:
Input : 5!
Processing : 5*4*3*2*1
Output : 120
Input : 6!
Processing : 6*5*4*3*2*1
Output : 720
Example 1:
Java
import java.io.*;
import java.util.*;
class GFG {
static int factorial( int n)
{
if (n == 0 || n == 1 )
return 1 ;
return n * factorial(n - 1 );
}
public static void main(String[] args)
{
int ans = factorial( 5 );
System.out.println( "Factorial of 5 is :" + ans);
}
}
|
Output
Factorial of 5 is :120
Time Complexity: O(n),The time complexity of the above code is O(n) as the recursive function is called n times.
Space Complexity: O(n),The space complexity is also O(n) as the recursive stack of size n is used.
Output explanation:
Initially, the factorial() is called from the main method with 5 passed as an argument. Since 5 is greater than or equal to 1, 5 is multiplied to the result of factorial( ) where 4 (n -1) is passed. Since, it is called from the same method, it is a recursive call. In each recursive call, the value of argument n is decreased by 1 until n reaches less than 1. When the value of n is less than or equal to 1, then there is no recursive call and at last, it returns 1.
Example 2:
Java
import java.io.*;
import java.util.*;
class GFG {
static int factorial( int n)
{
if (n == 0 || n == 1 )
return 1 ;
return n * factorial(n - 1 );
}
public static void main(String[] args)
{
int ans1 = factorial( 0 );
int ans2 = factorial( 1 );
System.out.println( "Factorial of 0 is : " + ans1);
System.out.println( "Factorial of 1 is : " + ans2);
}
}
|
Output
Factorial of 0 is : 1
Factorial of 1 is : 1
Time Complexity: O(n)
The time complexity of the above program is O(n). This is because in the worst case, the recursive calls are made n times, where n is the number for which the factorial is calculated.
Space Complexity: O(n)
The space complexity of the above program is O(n). This is because the additional recursive calls need to be stored in the call stack, which takes up O(n) space in the worst case.
Output explanation:
Initially, the factorial( ) is called from the main method with 6 passed as an argument. In each recursive call, the value of argument n is decreased by 1 until n reaches less than 1. For 1! decreased argument been passed is 0! as a small recursion call to be computed. As discussed above factorial of zero is 1. Hence, the small answer returned is 1, which later is multiplied to 1 itself so do result 1 as an output. Hence, it is stated when the value of n is less than or equal to 0 then no recursion call is computed as we will encounter negative integers over which factorial is not allowed to be computed.
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...