# 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 parameter`n`

.step 3: Inside the

`factorial`

function, initialize a variable called`res`

to 1.step 4: Use a

`for`

loop to iterate over numbers from 2 to`n`

(inclusive), with a variable called`i`

.step 5: Inside the

`for`

loop, multiply the`res`

variable with the current value of`i`

, and store the result back in the`res`

variable.step 6: After the

`for`

loop, return the value of the`res`

variable.step 7: Declare a variable called

`num`

and assign it a value of 6.step 8: Call the

`factorial`

function with the`num`

variable as an argument, and store the result in a variable called`factorialResult`

.step 9: Use

`console.log`

to print the string "Factorial of ", followed by the value of`num`

, followed by the string " is ", followed by the value of`factorialResult`

.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 parameter`n`

.step 3: Inside the

`factorial`

function, check if the value of`n`

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 of`n-1`

by calling the`factorial`

function recursively, and multiply the result with the value of`n`

.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 the`num`

variable as an argument, and store the result in a variable called`factorialResult`

.step 9: Use

`console.log`

to print the string "Factorial of ", followed by the value of`num`

, followed by the string " is ", followed by the value of`factorialResult`

.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 onHashnode - Gaurav Patil

Twitter - @GauravYPatil