Factorial of a number

easy

By - Aman Pareek

Last Updated - 26/08/2024

Problem Statement

Write a JavaScript function to find factorial of a number. You can use recursive approach, tail-recursive approach and iterative approach

You can understand factorial on byjus website

Example 1

Input: n = 5

Output: 120

Example 2

Input: n = 22

Output: 1124000727777607680000

Solution 1: Recursive Approach

function factorialRecursive(number) {
    if (number < 0) {
        throw new Error("Factorial cannot be calculated for negative values.");
    }
    if (number === 0 || number === 1) {
        return 1;
    }
    return number * factorialRecursive(number - 1);
} 

const n1 = 5;
factorialRecursive(n1);  //output: 120 

const n2 = 22;
factorialRecursive(n2);  //output: 1124000727777607680000 

Solution 2: Iterative Approach

function factorialIterative(number) {
    if (number < 0) {
        throw new Error("Factorial cannot be calculated for negative values.");
    }
    let product = 1;
    for (let i = 2; i <= number; i++) {
        product *= i;
    }
    return product;
} 

const n1 = 5;
factorialIterative(n1);  //output: 120 

const n2 = 22;
factorialIterative(n2);  //output: 1124000727777607680000 

Solution 3: Tail-Recursive

function factorialTailRecursive(number, accumulator = 1) {
    if (number < 0) {
        throw new Error("Factorial cannot be calculated for negative values.");
    }
    if (number === 0 || number === 1) {
        return accumulator;
    }
    return factorialTailRecursive(number - 1, accumulator * number);
} 

const n1 = 5;
factorialTailRecursive(n1);  //output: 120 

const n2 = 22;
factorialTailRecursive(n2);  //output: 1124000727777607680000 

Solution 4: Using array map reduce

function factorialArrayMapReduce(number) {
  if (number < 0) {
    throw new Error("Factorial cannot be calculated for negative values.");
  }
  return [...Array(number + 1).keys()]
    .slice(1)
    .reduce((product, current) => product * current, 1);
} 

const n1 = 5;
factorialArrayMapReduce(n1);  //output: 120 

const n2 = 22;
factorialArrayMapReduce(n2);  //output: 1124000727777607680000 

Solution 5: Using while loop

function factorialWhileLoop(number) {
    if (number < 0) {
        throw new Error("Factorial cannot be calculated for negative values.");
    }
    let product = 1;
    let i = 2;
    while (i <= number) {
        product *= i;
        i++;
    }
    return product;
} 

const n1 = 5;
factorialWhileLoop(n1);  //output: 120 

const n2 = 22;
factorialWhileLoop(n2);  //output: 1124000727777607680000 

Resources

Frequently asked questions

  1. What is factorial?

    See the factorial of a number n, written as n!, is found by multiplying all whole numbers from 1 up to n.

    For example:

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

    • 3!=3×2×1=6

  2. What is the factorial of 5?

    Factorial of 5 is 120. You can run these functions to check.

  3. Can we calculate factorial of a negative integer.

    No factorial of a number can not be calculated for negative integers. Only Positive integers

More Problems Solutions

Sum of Digits

Popular Solutions