Skip to content

Commit cf692b2

Browse files
Update Factorial of a Number.js
1 parent 58db26c commit cf692b2

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

JavaScript Interview Programs/Factorial of a Number.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@
22
// ****************************************************************************** Factorial *******************************************************************************
33

44
// 1. Find Factorial of a Number:
5-
6-
// Find factorial using For loop:
5+
// Find factorial using loop:
76
function factorial(n) {
87
let fact = 1;
98
for (let i = 1; i <= n; i++) {
10-
fact *= i;
11-
// fact = fact * i;
9+
fact *= i; // fact = fact * i;
1210
}
1311
return fact;
1412
}
1513
console.log("Factorial using For Loop:", factorial(5));
16-
1714
// Output:
18-
// Factorial using For Loop: 120
19-
20-
// OR
15+
// Factorial (using For Loop): 120
2116

2217
// Find factorial Using Recursion:
2318
function factorial(n) {
2419
if (n <= 1) return 1;
2520
return n * factorial(n - 1);
2621
}
2722
console.log("Factorial using Recursion:", factorial(5));
23+
// Output:
24+
// Factorial using Recursion: 120
2825

26+
// OR
27+
28+
// Find factorial Using Recursion:
29+
function factorial(n) {
30+
if (n === 0 || n === 1) {
31+
return 1;
32+
} else {
33+
return n * factorial(n - 1);
34+
}
35+
}
36+
const number = 5;
37+
const result = factorial(number);
38+
console.log("Factorial of", number, "is", result);
2939
// Output:
3040
// Factorial using Recursion: 120

0 commit comments

Comments
 (0)