diff --git a/Recursive/factorial.js b/Recursive/factorial.js new file mode 100644 index 000000000..0b1260c30 --- /dev/null +++ b/Recursive/factorial.js @@ -0,0 +1,16 @@ +// function to find factorial using recursion +// example : +// 5! = 1*2*3*4*5 = 120 +// 2! = 1*2 = 2 + +const factorial = (n) => { + if (n === 0) { + return 1 + } + return n * factorial(n - 1) +} + +// testing +console.log(factorial(4)) +console.log(factorial(15)) +console.log(factorial(0))