Skip to content

Commit f969b46

Browse files
Update Factorial.js
1 parent bbda638 commit f969b46

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
// ******************************************************** Factorial **************************************************
33

4-
// 1. Factorial of a number:
5-
// Using For Loop:
4+
// 1. Find Factorial of a Number:
5+
// Find factorial using loop:
66
function factorial(n) {
77
let fact = 1;
88
for (let i = 1; i <= n; i++) {
@@ -11,3 +11,16 @@ function factorial(n) {
1111
return fact;
1212
}
1313
console.log("Factorial (using For Loop):", factorial(5));
14+
// Output:
15+
// Factorial (using For Loop): 120
16+
17+
// OR
18+
19+
// Find factorial Using Recursion:
20+
function factorial(n) {
21+
if (n <= 1) return 1;
22+
return n * factorialRec(n - 1);
23+
}
24+
console.log("Factorial using Recursion:", factorial(5));
25+
// Output:
26+
// Factorial using Recursion: 120

0 commit comments

Comments
 (0)