Last active
March 31, 2022 17:26
-
-
Save AnkitMaheshwariIn/022013b75122a6b425eec3f28e8afe74 to your computer and use it in GitHub Desktop.
program to find the factorial of a number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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