Factorial of a Number in Javascript
We will discuss two methods to solve this problem using Javascript

I am a Full stack developer.
Hi everyone today we find the Factorial of a Number in Javascript. let's discuss how to solve this problem statement. if you want to check out yesterday's problem click here. We will discuss two methods to solve this problem using Javascript
Method 1: Iterative approach for factorial
Method 2: Recursive approach for factorial
Method 1: Iterative approach for factorial
Algorithm
step 1: Start the program.
step 2: Declare a function called
factorialthat takes a single parametern.step 3: Inside the
factorialfunction, initialize a variable calledresto 1.step 4: Use a
forloop to iterate over numbers from 2 ton(inclusive), with a variable calledi.step 5: Inside the
forloop, multiply theresvariable with the current value ofi, and store the result back in theresvariable.step 6: After the
forloop, return the value of theresvariable.step 7: Declare a variable called
numand assign it a value of 6.step 8: Call the
factorialfunction with thenumvariable as an argument, and store the result in a variable calledfactorialResult.step 9: Use
console.logto print the string "Factorial of ", followed by the value ofnum, followed by the string " is ", followed by the value offactorialResult.step 10: End the program.
Javascript Code
// Method to find factorial of the given number
function factorial(n) {
let res = 1;
for (let i = 2; i <= n; i++) {
res *= i;
}
return res;
}
// Driver method
let num = 6;
console.log("Factorial of " + num + " is " + factorial(num));
Output
Factorial of 6 is 720
Method 2: Recursive approach for factorial
Algorithm
step 1: Start the program.
step 2: Declare a function called
factorialthat takes a single parametern.step 3: Inside the
factorialfunction, check if the value ofnis equal to 0 using the===operator.step 4: If
nis equal to 0, return 1.step 5: If
nis not equal to 0, calculate the factorial ofn-1by calling thefactorialfunction recursively, and multiply the result with the value ofn.step 6: Return the result.
step 7: Declare a variable called
numand assign it a value of 6.step 8: Call the
factorialfunction with thenumvariable as an argument, and store the result in a variable calledfactorialResult.step 9: Use
console.logto print the string "Factorial of ", followed by the value ofnum, followed by the string " is ", followed by the value offactorialResult.step 10: End the program.
Javascript Code
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
// Driver method
let num = 6;
console.log("Factorial of " + num + " is " + factorial(num));
Output
Factorial of 6 is 720
Conclusion
So this article we solved the problem statement using javascript language. I hope you found this article helpful for your web development journey.
If you fill this article informative please like this article, if you have any suggestions please comment on this article, share it on social media and follow me on
Hashnode - Gaurav Patil
Twitter - @GauravYPatil




