Tuesday 26 November 2013

JAVASCRIPT : Factorial of a Number

JavaScript : Factorial of a Number :

Here is the code for the Factorial of a number in JavaScript code :

<script type = "text/javascript">
            var n = parseInt(window.prompt("Enter Number To Find Its Factorial:"));
            var result = fact(n);
            window.alert("Factorial of the given number " + result);
            function fact(n)
            {
                if(n == 0)
                    return 1;
                else 
                    return (n*fact(n-1));
            }
</script>

Output :

Enter Number To Find Its Factorial: 3
3 ! =  6 

No comments:

Post a Comment