We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bbda638 commit f969b46Copy full SHA for f969b46
1 file changed
JavaScript Interview Programs/Factorial.js
@@ -1,8 +1,8 @@
1
2
// ******************************************************** Factorial **************************************************
3
4
-// 1. Factorial of a number:
5
-// Using For Loop:
+// 1. Find Factorial of a Number:
+// Find factorial using loop:
6
function factorial(n) {
7
let fact = 1;
8
for (let i = 1; i <= n; i++) {
@@ -11,3 +11,16 @@ function factorial(n) {
11
return fact;
12
}
13
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
26
+// Factorial using Recursion: 120
0 commit comments