Skip to content
C++ Better Explained
Go back
C++ Factorial Program: Loop and Recursion Methods Explained
Edit page

C++ Factorial Program: Loop and Recursion

The factorial of a number is the product of every whole number from 1 up to that number. We write it with an exclamation mark: 5! means 5 × 4 × 3 × 2 × 1 = 120. Factorials show up everywhere in math and are a perfect way to practice both loops and recursion in C++.


Understanding Factorials

By definition, n! multiplies all integers from 1 through n. So 4! = 24, 6! = 720, and the special case 0! = 1. That last rule looks odd but it keeps the math consistent and gives recursion a clean stopping point.


Method 1: Using a Loop

The most direct way is to start with a running product of 1 and multiply it by each number up to n:

#include <iostream>

int main() {
    int n;
    std::cout << "Enter a number: ";
    std::cin >> n;

    unsigned long long factorial = 1;
    for (int i = 2; i <= n; i++) {
        factorial *= i;  // multiply running total by i
    }

    std::cout << n << "! = " << factorial << "\n";
    return 0;
}

We start the loop at 2 because multiplying by 1 changes nothing. Using unsigned long long instead of int lets us reach much larger results before overflowing. This loop version is efficient because it uses constant memory no matter how large n is.


Method 2: Using Recursion

A recursive function calls itself with a smaller input until it hits a base case. Factorial maps onto recursion beautifully because n! = n × (n-1)!.

#include <iostream>

unsigned long long factorial(int n) {
    if (n <= 1) return 1;          // base case: 0! and 1! are 1
    return n * factorial(n - 1);   // recursive step
}

int main() {
    std::cout << "5! = " << factorial(5) << "\n";  // 120
    return 0;
}

The base case n <= 1 is essential. Without it, the function would call itself forever and crash with a stack overflow. Each call waits for the smaller call to finish, then multiplies the result by n on the way back up.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Loop vs Recursion: Which to Use

Both produce identical answers, so the choice comes down to style and constraints. The loop version uses a fixed amount of memory and is slightly faster, making it the safer default. The recursive version reads almost exactly like the mathematical definition, which makes the logic easy to understand. For very large n, prefer the loop — deep recursion can exhaust the call stack.

AspectLoopRecursion
MemoryConstantGrows with n
SpeedSlightly fasterSlight call overhead
ReadabilityClearMatches the math
RiskNoneStack overflow if n is huge

Watch Out for Overflow

Factorials explode in size. A plain int can only hold up to 12! (479,001,600). Even unsigned long long overflows past 20!. If your program silently returns a wrong or negative number for larger inputs, overflow is almost always the cause. For genuinely big factorials you would need a big-integer library or a different representation.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ endl vs \n: What's the Difference and Which to Use
Next Post
C++ getline: How to Read a Full Line of Input (With Spaces)