... (nonrecursively) by using a `for` statement as follows:

``````
factorial = 1;
for (unsigned int counter{number}; counter >= 1; --counter) {
factorial *= counter;
}
``````

# Recursive Factorial

A recursive definition of the factorial function is arrived at by observing the following algebraic relationship:

``````
n! = n · (n – 1)!
``````

For example, 5! is clearly equal to 5 * 4! as is shown by the following:

``````
5! = 5 ·  4 · 3 · 2 · 1
5! = 5 · (4 · 3 · 2 · 1)
5! = 5 · (4!)
``````

# Evaluating 5!

The evaluation of 5! would proceed as shown in Fig. 6.24, which illustrates how the succession of recursive calls proceeds until 1! is evaluated to be 1, terminating the recursion. Figure 6.24(b) shows the values returned from each recursive call to its caller until the final value is calculated ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.