How can I compute a factorial?

Question

How can I compute a factorial?

Answer

A factorial is calculated as a product of all of the numbers up to the given number. If we try factorial(4), for example, the result is the product of all numbers up to 4: 4 * 3 * 2 * 1.
Knowing this, we can use our input as the number of times a loop will run. A while loop is a popular choice for solving this because it will loop until our number reaches our stopping point, which is 0.

4 Likes