Factorial of a number.

In this JavaScript program, we will learn to calculate the factorial of a number. It represents the multiplication of all numbers between 1 and n. It is denoted by !.

Example

5! = 5*4*3*2*1 =  120.

Program

<html>
    <head>       
    </head>
    <body>        
        <h1 style="color:green;text-align: center;margin-top: 20px;" id="val"></h1>
    </body>
    <script>
        function getFact(n){
            var fact = 1;
            while(n>0){
                fact = fact*n;
                n--;
            }
            return fact;
        }         
        document.getElementById('val').innerHTML= "Factorial of 5 = "+getFact(5);
    </script>
</html>

Output: Factorial of 5 = 120

 

 

 

Back to top button