Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Factorial of a number using JavaScript

  • Last Updated : 11 Oct, 2019

Given a positive integer n and the task is to find the factorial of that number with the help of javaScript.
factorial of number

Examples:

Input : 4
Output : 24

Input : 5
Output : 120

Approach 1: Iterative Method In this approach, we are using a for loop to iterate over the sequence of numbers and get the factorial.

Example:




<!DOCTYPE HTML> 
<html
  
<head
    <title
        Factorial of a number using JavaScript
    </title>
</head
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
      
    <p id = "GFG_UP" style =
        "font-size: 15px; font-weight: bold;">
    </p>
          
    <button onclick = "GFG_Fun()">
        Click Here
    </button>
      
    <p id = "GFG_DOWN" style =
        "color:green; font-size: 20px; font-weight: bold;">
    </p>
          
    <script>
        var up = document.getElementById('GFG_UP'); 
        var down = document.getElementById('GFG_DOWN'); 
        var n = 5;
          
        up.innerHTML = "Click on the button to calculate"
                + " the factorial of n.<br>n = " + n;
          
        function Factorial(n) {
            var ans=1;
              
            for (var i = 2; i <= n; i++)
                ans = ans * i;
            return ans;
        }
          
        function GFG_Fun() { 
            down.innerHTML = Factorial(n);
        
    </script
</body
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2: Recursive Method: In this approach, we are calling the same function again and again to get the factorial of a number.

Example:




<!DOCTYPE HTML> 
<html
  
<head
    <title
        Factorial of a number
        using JavaScript
    </title>
</head
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <button onclick = "GFG_Fun()">
        Click Here
    </button>
      
    <p id = "GFG_DOWN" style
        "color:green; font-size: 20px; font-weight: bold;">
    </p>
      
    <script>
        var up = document.getElementById('GFG_UP'); 
        var down = document.getElementById('GFG_DOWN'); 
        var n = 10;
          
        up.innerHTML = "Click on the button to calculate"
                + " the factorial of n.<br>n = " + n;
          
        function Factorial(n) {
            if (n === 0) { 
                return 1; 
            }
            else { 
                return n * Factorial( n - 1 ); 
            }
        }
          
        function GFG_Fun() { 
            down.innerHTML = Factorial(n);
        
    </script
</body
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!