Factorial Program in C – Iterative and Recursive with Output

The factorial program in C is one of the most common exercises for learning recursion and loops. The factorial of a non-negative integer n, written n!, is the product of all positive integers from 1 to n. By definition, 0! = 1.

This page covers two approaches — iterative and recursive — with compile-verified code, sample output, and notes on integer overflow.

What Is Factorial?

n! = n × (n−1) × (n−2) × … × 2 × 1

Examples:

  • 5! = 5 × 4 × 3 × 2 × 1 = 120
  • 0! = 1 (by definition)
  • 1! = 1

Iterative Factorial Program in C

The iterative version uses a for loop to accumulate the product:

#include <stdio.h>

long long factorial(int n)
{
    long long result = 1;
    int i;
    for (i = 2; i <= n; i++)
        result *= i;
    return result;
}

int main(void)
{
    int n;
    printf("Enter a non-negative integer: ");
    scanf("%d", &n);

    if (n < 0)
        printf("Factorial is not defined for negative numbers.\n");
    else
        printf("%d! = %lld\n", n, factorial(n));

    return 0;
}

Recursive Factorial Program in C

The recursive version mirrors the mathematical definition directly:

#include <stdio.h>

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

int main(void)
{
    int n;
    printf("Enter a non-negative integer: ");
    scanf("%d", &n);

    if (n < 0)
        printf("Factorial is not defined for negative numbers.\n");
    else
        printf("%d! = %lld\n", n, factorial(n));

    return 0;
}

How the recursion works for n = 4:

factorial(4) = 4 × factorial(3)
             = 4 × 3 × factorial(2)
             = 4 × 3 × 2 × factorial(1)
             = 4 × 3 × 2 × 1
             = 24

The base case n <= 1 handles both 0! and 1! — both return 1 without further recursion.

How to Compile and Run

gcc -Wall -o factorial factorial.c
./factorial

long long and %lld require C99 or later (the default for modern GCC). No flags needed beyond -Wall.

Sample Output

Enter a non-negative integer: 5
5! = 120

Enter a non-negative integer: 0
0! = 1

Enter a non-negative integer: 10
10! = 3628800

Factorial Table — 0! to 15!

n n!
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5,040
8 40,320
9 362,880
10 3,628,800
12 479,001,600
15 1,307,674,368,000
20 2,432,902,008,176,640,000

Integer Overflow Warning

long long holds up to 9,223,372,036,854,775,807. This means:

  • 20! = 2,432,902,008,176,640,000 — fits in long long
  • 21! = 51,090,942,171,709,440,000 — overflows long long

For factorials beyond 20, you need arbitrary-precision arithmetic (a GMP library, or an array-based big integer). Most interview questions stay within the long long range.

Time and Space Complexity

Version Time Space
Iterative O(n) O(1)
Recursive O(n) O(n) — call stack depth

Both do n multiplications. The recursive version uses O(n) stack space because each call waits for the next to return before it can multiply. For very large n, iterative is safer (no stack overflow risk).

What This Program Teaches

  • How recursion maps directly onto mathematical definitions — factorial is the canonical example
  • The base case: without if (n <= 1) return 1 the recursion never terminates
  • Why 0! = 1 matters: it makes the base case consistent and enables formulas like the binomial coefficient to work for edge cases
  • The trade-off between iterative (O(1) space, no stack risk) and recursive (elegant, O(n) space) solutions

Related Programs


As an Amazon Associate we earn from qualifying purchases.

Recommended Book

Recursion and loops are covered in depth in The C Programming Language by Kernighan & Ritchie — the standard reference for learning C properly. Also on Amazon.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>