writing javaScript code to find factorials of a number

In this javaScript tutorial we are writing javaScript code to find factorials of a number

factorial

factorial is multiplication of all whole numbers, it is nothing but multiplication of all numbers less than that number up to 1. 

factorial number is denoted by ! this symbol.

a simple way For calculating factorial numbers through programming is traversing all numbers and multiplying it to the initial resultant variable.  

We will travel 5-1 to find a factorial of 5!, it means traveling all numbers between 5-1 and multiplying all the numbers.

Basically for implementing this kind of logic we use loop statements.

If the resultant variable may contain zero or negative value that means whole logic is wrong , because the factorial is always calculating in positive number.


writing javaScript code to find factorials of a number

<html>
<head>
    <script type="text/javascript">
    function show()
        {
            var
            i,no,fact;
            fact=1;
            no=Number(document.getElementById('num').value);
            for(i=1;i<=no;i++)
                {
                    fact=fact*i;
                }
            document.getElementById("answer").value=fact;
        }
    </script>
    </head>
    <body>
    enter number:<input  id="num">
    <button onclick="show()">factorial</button>
    <input id="answer">
    </body>
</html>

output

writing javaScript code to find factorials of a number

Post a Comment

0 Comments