Factorial using Recursion in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>

long factorial(int n) {
    if(n==0) return 1;
    return n * factorial(n-1);
}

int main() {
    int n = 5;
    printf("Factorial of %d is %ld\n", n, factorial(n));
    return 0;
}

C Output

Input:
5

Output:
Factorial of 5 is 120


C++ Program

#include <iostream>
using namespace std;

long factorial(int n) {
    if(n==0) return 1;
    return n * factorial(n-1);
}

int main() {
    int n = 6;
    cout << "Factorial of " << n << " is " << factorial(n);
    return 0;
}

C++ Output

Input:
6

Output:
Factorial of 6 is 720


JAVA Program

public class Main {
    static long factorial(int n) {
        if(n==0) return 1;
        return n * factorial(n-1);
    }
    public static void main(String[] args) {
        int n = 4;
        System.out.println("Factorial of " + n + " is " + factorial(n));
    }
}

JAVA Output

Input:
4

Output:
Factorial of 4 is 24


Python Program

def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n-1)

n = 7
print(f"Factorial of {n} is {factorial(n)}")

Python Output

Input:
7

Output:
Factorial of 7 is 5040


Explanation
Example
The factorial of a number n is actually multiplying all the numbers from n down to 1. So in the case of 5, it is 5 × 4 × 3 × 2 × 1 = 120. In recursion, we don't write a loop, but call the function with a smaller value until it comes to the simplest case, which is called the base case. Here, the base case is n == 0, and it returns 1. The rest of the calculation occurs naturally because each function call makes the current n multiply with the factorial of (n-1).

Real-Life Analogy
Recursion is similar to piling up boxes. If you need to open the bottom box, you remove the topmost first, then the next, till you eventually get to the bottom box. As soon as you reach the bottom, you reassemble the boxes in reverse order. In terms of factors, each "box" is a step of multiplication. You dismantle the problem into manageable, easy parts until it can no longer be broken down, and then you assemble the answer bottom to top. 

Why It Matters
Recursion shows you how to work through problems by dividing them into smaller subproblems. It is particularly useful for problems with a naturally repetitive, self-similar structure, e.g., visiting trees, solving mazes, or doing divide-and-conquer algorithms such as merge sort and quicksort. Recursion is used to teach factorial so that more intricate recursive solutions later on are easier to understand.

Complexity and Optimization
The recursive factorial has time complexity of O(n) since we are calling once for every number from n down to 0. The space complexity is O(n) because every call adds an additional frame on the call stack until we hit the base case. Even though iterative solutions can make the space complexity O(1), recursion is sometimes cleaner and easier to read for small inputs. For extremely large inputs, though, recursion can lead to a stack overflow, so tail recursion or iterative approaches would be more appropriate.

Edge Cases and Testing
The minimum input is 0, where the factorial equals 1 by definition. Negative numbers have no factorial in standard integer arithmetic, so must be treated specially, usually by returning a error message or throwing an exception. Small numbers such as 1 and big numbers must also be tested for correctness and efficiency. 

Interview Tips
Interviewers also like to use the factorial problem in order to verify a candidate's grasp of recursion, base cases, and how stacks behave. They might stretch it out and have you convert the recursive function into an iterative one or describe step by step how stack memory is being used. They may request that you implement edge cases or optimize the strategy.

SEO-Friendly Closing
The recursive factorial program is the most common beginner coding exercise across C, C++, Java, and Python. It aids novice programmers in learning function calls, base cases, and usage of the stack while solidifying mathematical reasoning. Recursive implementation of factorial not only enhances problem-solving but also gets you ready for real-world applications such as traversal in data structures, backtracking algorithms, and recursive mathematical calculations. By coding this program in several languages, you establish a solid foundation for software development tasks, competitive programming, and coding interviews.