Factorial (Recursive) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int fact(int n){
    if(n<=1) return 1;
    return n*fact(n-1);
}
int main(){
    int n=5;
    printf("%d",fact(n));
}

C Output

Input:  
n = 5 

Output:  
120


C++ Program

#include <bits/stdc++.h>
using namespace std;
int fact(int n){
    if(n<=1) return 1;
    return n*fact(n-1);
}
int main(){
    int n=6;
    cout<<fact(n);
}

C++ Output

Input:  
n = 6 

Output:  
720


JAVA Program

class Main{
    static int fact(int n){
        if(n<=1) return 1;
        return n*fact(n-1);
    }
    public static void main(String[] args){
        int n=4;
        System.out.print(fact(n));
    }
}

JAVA Output

Input:  
n = 4  

Output:  
24


Python Program

def fact(n):
    if n<=1:
        return 1
    return n*fact(n-1)

n=7
print(fact(n))

Python Output

Input:  
n = 7 

Output:  
5040


Detailed Explanation
Problem Statement
We need to calculate the factorial of a number by using recursion. In recursion, a function calls itself recursively until it comes across a base case. The factorial of n is:


n! = n × (n-1) × (n-2) ×. × 1
and 0! = 1 by definition.

Step-by-Step Dry Run (C Example: n = 5)
fact(5) → returns 5 * fact(4)

fact(4) → returns 4 * fact(3)

fact(3) → returns 3 * fact(2)

fact(2) → returns 2 * fact(1)

fact(1) → base case, returns 1

Now the return values multiply when the stack unwinds:
1 → 2 → 6 → 24 → 120.

Real-Life Analogy
Suppose a chef is making a layered cake. To prepare the top layer, they need to finish off the one below. Every layer relies on the previous layer's completion. When the bottom is done (base case), the chef can now build upwards until the entire cake is complete.

Why Recursion Works Here
Factorial admits a natural recursive definition in math. Recursion directly corresponds to that definition, and so the code is shorter and more readable compared to the iterative one. The biggest disadvantage is function call overhead.

Complexity
Time Complexity: O(n) since we are making n recursive calls.

Space Complexity: O(n) due to the call stack that contains the intermediate states.

Common Mistakes
Missing the base case, resulting in infinite recursion.

Implementing if(n==0) return 0; rather than returning 1 for 0!.

Employing recursion for extremely large numbers (risk of stack overflow).

Practical Applications
Factorials play a fundamental role in permutation and combination problems, probability theory, and some algorithms such as binomial coefficients in Pascal's triangle. Recursion as a concept is also employed extensively in tree traversal, backtracking algorithms, and divide-and-conquer methods.

Learning Insights
This problem helps beginners learn how recursion mirrors mathematical definitions, understand the importance of base cases, and visualize the call stack in action. It’s a gentle way to step into more advanced recursive algorithms.

SEO-Optimized Closing Paragraph
The recursive factorial is a starting-point practice for mastering recursion programming. It breaks down the mathematical expression into neat, readable code while illustrating the base case and function calling concepts. Recursive factorial practice in C, C++, Java, and Python helps beginners develop a solid grasp of recursion mechanics, call stack behavior, and solution approaches to be applied to even more advanced tasks such as tree traversal, dynamic programming, and backtracking in competitive programming and interviews.