Factorial in C: solved exercise

Factorial in C: solved exercise

If you searched for a solved factorial exercise in C, here are three implementations with overflow analysis: the classic recursive version, the iterative version, and a precomputed lookup table for O(1) queries.

Factorial is the first exercise where integer overflow becomes a real problem: 13! already exceeds the range of a 32-bit int, and 21! exceeds long long.

Problem statement

Implement three versions of the factorial function:

  1. fact_recursive(n): using recursion.
  2. fact_iterative(n): using a loop.
  3. A precomputed fact_table[21] for instant lookups.

Print the factorials of 0 through 20 using each version.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>

/* Recursive */
long long fact_recursive(int n) {
    if (n <= 1) return 1;
    return n * fact_recursive(n - 1);
}

/* Iterative */
long long fact_iterative(int n) {
    long long r = 1;
    for (int i = 2; i <= n; i++) r *= i;
    return r;
}

/* Precomputed table (valid up to 20!) */
static long long fact_table[21];

void precompute(void) {
    fact_table[0] = 1;
    for (int i = 1; i <= 20; i++)
        fact_table[i] = fact_table[i - 1] * i;
}

int main(void) {
    precompute();
    printf("%-5s %-20s %-20s %-20s\n", "n", "Recursive", "Iterative", "Table");
    for (int i = 0; i <= 20; i++) {
        printf("%-5d %-20lld %-20lld %-20lld\n",
               i, fact_recursive(i), fact_iterative(i), fact_table[i]);
    }
    return 0;
}

Expected output

1
2
3
4
5
6
7
n     Recursive            Iterative            Table               
0     1                    1                    1                   
1     1                    1                    1                   
2     2                    2                    2                   
3     6                    6                    6                   
...
20    2432902008176640000  2432902008176640000  2432902008176640000 

Common mistakes

  • Using int instead of long long: 13! = 6,227,020,800 exceeds the int maximum (2,147,483,647). The result silently wraps to a negative or incorrect value.
  • Not handling n = 0: by definition 0! = 1; without a base case the function returns 0.
  • Computing factorials of negative numbers without validation: this causes undefined behavior in the recursive implementation.
  • Relying on the recursive version for large n: stack depth grows linearly and can cause a stack overflow on systems with a small stack.

Practical use

Factorial appears in combinatorics (permutations, binomial coefficients), in the gamma function, in brute-force algorithm analysis, and in permutation generation. The precomputed table is the standard pattern when the domain is small and queries are frequent.

Guided practice and full book

If you want a complete path with progressive difficulty:

FAQ

Why does 13! give a negative number when using int?

Because 13! = 6,227,020,800 exceeds the maximum value of a 32-bit int (2,147,483,647). Signed integer overflow is undefined behavior in C, but in practice the most significant bits are discarded, producing a negative value.

What happens with 21! in long long?

21! = 51,090,942,171,709,440,000 exceeds the long long range (9,223,372,036,854,775,807). For larger factorials you need arbitrary-precision arithmetic (GCC’s __int128 reaches up to 33!) or a big-integer library.

Is the recursive or iterative version more efficient?

The iterative version: it does not create extra stack frames and the compiler can optimize it easily. The recursive version has the same O(n) complexity but carries function call overhead. For factorials the difference is negligible in practice given the small valid domain (0–20 with long long).