Algorithm 02: Factorialize a Number

In this algorithm we will see how to take a number, then find its factorial.  If you forgot from your math class what a factorial is it is the product of an integer and all the integers below.  For example, the factorial of 5 would be: 5! = 1 * 2 * 3 * 4 * 5 = 120.

One way factorials are useful in the real world would be to find out how many different ways a set number of objects can be arranged. Back to our example, if we have a group of 5 people and we want to find all the different combinations of how we can line them up in a row, there would be 5! = 120 different ways they could be lined up.

The key to solving this algorithm is recursion.  We call the same function over and over again until we meet the condition that num === 0.

Like to leave a comment?

Comments have been automatically closed on this post but don't let that stop you. Hit me up on following networks: