-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFactorial.js
More file actions
35 lines (27 loc) · 745 Bytes
/
Factorial.js
File metadata and controls
35 lines (27 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n.
// For example factorial of 6 is 6*5*4*3*2*1 which is 720.
// Approach 1(For loop)
function factorialWithFoorLoop(num) {
let result = 1;
for (let i = num; i > 0; i--) {
result *= i;
}
return result;
}
console.log(factorialWithFoorLoop(10));
// Approach 2(While loop)
function factorialWithWhileLoop(num) {
let result = 1;
while (num > 0) {
result *= num;
num--;
}
return result;
}
console.log(factorialWithWhileLoop(5));
// Approach 3(Recursive)
function factorialWithRecursion(num) {
if (num == 0) return 1;
else return num * factorialWithRecursion(num - 1);
}
console.log(factorialWithRecursion(10));