Function to compute factorial of a number in JavaScript



In this article, the given task is to get the factorial of a number in JavaScript.

What is the factorial of a number?

The multiplication of all positive integers smaller than or equal to n gives the factorial of a non-negative integer.

For example, assume the non-negative integer is 4 and the factorial of 4 will be 4*3*2*1 which is equal to 24. The symbol of factorial is represented by "!". so it will be (4!).

The value of 0! Will be 1 as 0 is not a positive integer.

The mathematical formula of factorial

("n!") This is how the factorial of a number is represented. it is the product of all positive integers which are less than or equal to n.

Below is the formula of factorial.

n! = n*(n-1)*(n-2)*……3*2*1 

Using for loop

We can calculate the factorial of a number by using for loop.

Algorithm

These are the steps to get the factorial of a given number by using for loop.

  • create a variable (result) with value 1.

  • Call the function fact(num).

  • If the num is less than 0, will return -1.

  • If the num = 0, will return 1.

  • If the num > 1, with each iteration it will decrease the value by 1 and multiply by the result.

  • The factorial result will be stored in the result variable.

Example

Following is the program to get the factorial of a number by using the above for loop algorithm.

<!DOCTYPE html> <html> <head> <title>Factorial of a number</title> <button onClick = "func()"> Click to get factorial </button> <p id="para"> </para> <script> function func() { function fact(num) { if (num < 0){ return -1; } else if(num == 0){ return 1; } else { let result = 1; for(var i = num; i > 1; i--){ result *= i; }; return result; } }; const num = 4; document.getElementById("para").innerHTML = fact(num); }; </script> </head> </html>

As we can see in the output, it calculated the factorial of a number with the help of for loop by iterating the input number and decreasing it for every iteration.

Using recursion

We can get the factorial of a number by recursion.

Algorithm

Below are the following steps to achieve factorial of number by recursion.

  • Get the number from num.

  • Call the function factorial(num).

  • If num is less than 0, then return -1.

  • Else if num = 0 return 1.

  • Else, return (num * factorial(num - 1))

Example

Following is the example of getting the factorial of a number by using the above recursive algorithm −

<!DOCTYPE html> <html> <title>Factorial of a number</title> <head> <button onClick = "func()">Factorial</button> <p id="para"> </para> <script> function func(){ function factorial(num) { if (num < 0) { return -1; } else if (num == 0){ return 1; } else { return num * factorial(num - 1); } } let num = 4; document.getElementById("para").innerHTML = factorial(num); }; </script> </head> </html>

On executing the above program a button named Factorial is displayed. If you click on this button The factorial of the input number (4) is calculated and the result is printed.

Using While loop

We can achieve the factorial of a number by using while loop.

Algorithm

These are steps to calculate the factorial of a number by using while loop.

  • create variable res.

  • Call the function fact(num).

  • If the num is less than 0, will return -1.

  • If the num = 0, will return 1.

  • If the num > 1, with each iteration it will decrease the value by 1 and multiply by the result.

  • The factorial result will be stored in the result variable.

Example

Following is the example of getting the factorial of a number by using a while loop −

<!DOCTYPE html> <html> <head> <title>Factorial of a number</title> <button onClick = "func()">Click me! </button> <p id = "para"></para> <script> function func(){ function fact(num) { var res = num; if (num === 0){ return 1; } else if (num === 1){ return 1; } else{ while (num > 1) { num--; res *= num; } } return res; }; const num = 4; document.getElementById("para").innerHTML = fact(num); }; </script> </head> </html>

As we can see from the output, the while loop was used to calculate the factorial of a given number by iterating the input number and decreasing it each time.


Advertisements