Factorial of a Number in Javascript
We will discuss two methods to solve this problem using Javascript
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
factorial
that takes a single parametern
.step 3: Inside the
factorial
function, initialize a variable calledres
to 1.step 4: Use a
for
loop to iterate over numbers from 2 ton
(inclusive), with a variable calledi
.step 5: Inside the
for
loop, multiply theres
variable with the current value ofi
, and store the result back in theres
variable.step 6: After the
for
loop, return the value of theres
variable.step 7: Declare a variable called
num
and assign it a value of 6.step 8: Call the
factorial
function with thenum
variable as an argument, and store the result in a variable calledfactorialResult
.step 9: Use
console.log
to 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
factorial
that takes a single parametern
.step 3: Inside the
factorial
function, check if the value ofn
is equal to 0 using the===
operator.step 4: If
n
is equal to 0, return 1.step 5: If
n
is not equal to 0, calculate the factorial ofn-1
by calling thefactorial
function recursively, and multiply the result with the value ofn
.step 6: Return the result.
step 7: Declare a variable called
num
and assign it a value of 6.step 8: Call the
factorial
function with thenum
variable as an argument, and store the result in a variable calledfactorialResult
.step 9: Use
console.log
to 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