find factorial
of a number
[ recursion ]

Find factorial of a number

Write a function that returns the factorial for a given non-negative integer.


factorial

The factorial of a non-negative integer ( n ) is the product of all positive integers less than or equal to ( n ).

To find the factorial of a number multiply the number by every whole number less than it, down to 1.

n! = n × (n - 1) × (n - 2) × (n - 3) × ... × 1

For example...

5 factorial is calculated as 5 × 4 × 3 × 2 × 1 which equals 120

5 factorial can be written as 5!

Example ...

Enter a number to find its factorial ...

7 find 7 factorial

The function returns 5040 7 factorial

Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


To find the factorial of a number multiply the number by every whole number less than it, down to 1.

The factorial of 0 is defined as 1.

0! = 1

0! = 1

1! = 1

2! = 2 × 1 = 2

3! = 3 × 2 × 1 = 6

4! = 4 × 3 × 2 × 1 = 24

5! = 5 × 4 × 3 × 2 × 1 = 120

6! = 6 × 5 × 4 × 3 × 2 × 1 = 720

7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040


Factorials are useful for counting the number of ways to arrange, combine, or select distinct items.

For example, how many different ways can n things be arranged?

Example:

How many ways can three books be arranged in a row?

3! = 3 × 2 × 1 = 6

There are 6 ways to arrange three books in a row.

3! = 6


Find the factorial of a given number using recursion


Recursion The act of a function calling itself.

Recursion is used to solve problems that contain smaller sub-problems.

A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).

Use recursion to find the factorial of 5.

let x = 5;

function factorial(num) {

if (num > 1) { → Recursive call

return num * factorial(num - 1);

}

else { → Base case

return 1;

};

}

call function

factorial(x); returns ↴

120 factorial of 5 → 120


Initialize a variable to hold given number n of the factorial.

const number = n; → user input


Define a function factorial that calculates the factorial of a given number.

function factorial(num) {}

The function takes a non-negative number as input, num and returns its factorial.

The function checks for two Base cases:

Base case The condition under which the recursion stops.

If num equals 0 or 1

Factorial is not defined for negative numbers ↴

if (num < 0) return -1

Return -1 (indicating an error) and end execution of function.

Base case The condition under which the recursion stops.

Check if num is 0

else if (num == 0) return 1

Return 1 and end execution of function.

Recursive case num is greater than 1

factorial function calls itself with decremented values until it reaches the base case.

If num is not negative or 0

call the function recursively ↴

return num * factorial( num - 1)

Function recursively calls itself, multiplying the current number by the factorial of the number minus one.

This recursive call continues until it reaches one of the base cases.


Call the function with ↴

factorial(number);


Find the factorial of 5

num = 5;

function factorial(num)

factorial(5) 5 × factorial(4)

factorial(4) 4 × factorial(3)

factorial(3) 3 × factorial(2)

factorial(2) 2 × factorial(1)

factorial(1) 1 Base case

The function stacks calls, until it reaches the base case, then unwinds the stack to produce the result ↴

factorial(2) 2 × 12

factorial(3) 3 × 26

factorial(4) 4 × 624

factorial(5) 5 × 24120 result

5!  =  120


Find the factorial of 5

const number = 5;

function factorial(num) {

if (num < 0) return -1;

else if (num == 0) return 1;

else {

return num * factorial( num - 1);

}

}

call function

factorial(number); returns ↴

120


Alternative ↴

Find the factorial of 5

const number2 = 5;

function factorial2(num) {

if (num === 0 || num === 1) {

return 1;

}

return num * factorial2(num - 1);

}

call function

factorial2(number2); returns ↴

120


Alternative using a ternary operator ↴

const number3 = 5;

function factorial3(num) {

return num === 0 ? 1 : num * factorial3(num - 1);

}

call function

factorial3(number3); returns ↴

120

Find factorial

Enter number to find factorial