Skip to content

Instantly share code, notes, and snippets.

@AnkitMaheshwariIn
Last active March 31, 2022 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnkitMaheshwariIn/022013b75122a6b425eec3f28e8afe74 to your computer and use it in GitHub Desktop.
Save AnkitMaheshwariIn/022013b75122a6b425eec3f28e8afe74 to your computer and use it in GitHub Desktop.
program to find the factorial of a number
// program to find the factorial of a number
function findFactorial(num) {
// if number is 0
if (num === 0) {
return 1
} else {
// if number is positive,
// multiply number with response of function findFactorial(num - 1),
// having argument one less than num
return num * findFactorial(num - 1)
}
}
const num = 3; // declare and initialize a variable with value 3
// calling function findFactorial() if num is non-negative
if (num > 0) {
const result = findFactorial(num)
console.log(`The factorial of ${num} is ${result}`)
}
/*
THE OUTPUT IS:
"The factorial of 3 is 6"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment