find factorial
of a number
[ while-loop ]

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 a while loop


while loop repeatedly executes a block of code as long as a specified condition evaluates to true.

while (condition) {

// execute code as long as condition is true

}

let x = 0; → counter

while (x < 4) {

console.log(x);

x++;

}

Initialize a counter variable x outside of the loop.

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is printed to the console.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


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.

Initialize a variable with the value of num

let result = num result holds the accumulated result

Base case

If num equals 0 or 1 return 1

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

return 1 and end execution of the function.

Loop while num is greater than 1

while (num > 1) {}

Decrement num by 1 at each iteration.

num--

Multiply result by the decremented num

result *= num result = result * num

Return computed factorial value.

return result


Call the function with ↴

factorial(number);


Find the factorial of 5

num = 5;

function factorial(num)

The function checks if num is 0 or 1

If it is, it returns 1 and ends execution of the function.

If num is greater than 1 the function proceeds.

while (num > 1) {}

result *= num; result = result * num

First iteration of loop ↴

result5

num5

num-- num = num - 14

num Iteration ↴

4 result = 5 × 420

3 result = 20 × 360

2 result = 60 × 2120

1 Loop ends and the calculated factorial of 5 is returned ↴

5!  =  120


Find the factorial of 5

const number = 5;

function factorial(num) {

let result = num;

if (num === 0 || num === 1) return 1;

while (num > 1) {

num--;

result *= num;

}

return result;

}

call function

factorial(number); returns ↴

120

Find factorial

Enter number to find factorial