You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// *************************************************************** Generate Factorial of a Number in Different Ways **********************************************************
3
3
4
-
// 1. Find Factorial of a Number:
5
-
// Find factorial using loop:
6
-
functionfactorial(n){
4
+
// 1. Generate Factorial of a Number:
5
+
6
+
// factorial using for loop:
7
+
functionfactorial(n)
8
+
{
7
9
letfact=1;
8
-
for(leti=1;i<=n;i++){
10
+
for(leti=1;i<=n;i++)
11
+
{
9
12
fact*=i;// fact = fact * i;
10
13
}
11
14
returnfact;
12
15
}
13
16
console.log("Factorial using For Loop:",factorial(5));
14
17
// Output:
15
-
// Factorial (using For Loop): 120
18
+
// Factorial using For Loop: 120
16
19
17
-
// 2. Find factorial Using Recursion:
18
-
functionfactorial(n){
19
-
if(n<=1)return1;
20
+
// 2. factorial Using Recursion:
21
+
functionfactorial(n)
22
+
{
23
+
if(n<=1){
24
+
return1;
25
+
}
20
26
returnn*factorial(n-1);
21
27
}
22
28
console.log("Factorial using Recursion:",factorial(5));
@@ -25,16 +31,20 @@ console.log("Factorial using Recursion:", factorial(5));
0 commit comments