(bright upbeat music)
- [Narrator] Let's take a look at my solution
for calculating a factorial without recursion.
Here is 05solution.cpp from chapter five
of the exercise files.
This solution is very simple.
I've created a function that returns a unsigned long editor
and takes an unsigned long editor as its single parameter.
It uses a simple while loop
to calculate the product of all numbers, two through N.
And you'll notice that my condition is N
is greater than one,
because there's really no point in multiplying by one.
Any number multiplied by one results in itself.
And then we return the result.
I call the function down here with the number five
and when I build and run,
you'll notice that we get the correct answer.
This exercise is more about creating the function
than it is about the algorithm itself
although understanding the nonrecursive algorithm
does have its own value.