FACTORIAL

Question : Write a program to find a Factorial of a Number.

The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1.

Examples :

  • 4! = 4 * 3 * 2 * 1 = 24
  • 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
  • 1! = 1

Method 1

function factorial(n) {
let res = 1;
if (n == 0 || n == 1) return n;
else {
for (let i = n; i >= 1; i--) {
res *= i;
}
}
return res;
}
console.log(factorial(5)); //Prints: 120