Example 1: The Factorial of a Number

Recursively compute the factorial of a number.

We'll cover the following

This section covers some common examples of recursion. Go through them, one by one, to get a firm grasp on recursion.

The factorial of a number

The factorial of a number, nn, is the product of all the integers from 1 to nn. It is denoted as: n!n!.

n!=n(n1)(n2)...1n! = n*(n-1)*(n-2)*...*1

For example:

5!=543215! = 5*4*3*2*1
5!=1205! = 120

💡 Note: The factorial of both 00 and 11 is 11.

Let’s design a recursive approach to calculate the factorial.

Get hands-on with 1200+ tech skills courses.