31. JavaScript Program to find the Factorial of a given number.
How does this program work?
- This program is used to find the factorial of a given number using JavaScript.
- Factorial number is defined by the product of all the digits of given number.
- Factorial of 0 is always 1.
Here is the code
<html>
<head>
<title>JavaScript program to find the Factorial of a given number</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter a number"> </td>
</tr>
<tr>
<td> <button onclick="factorial ()" >Submit</button> </td>
</tr>
</table>
<div id="num"></div>
</body>
<script type="text/javascript">
function factorial()
{
var n,i,fact = 1;
n = parseInt(document.getElementById ("first").value);
for(i = 1; i <= n; i++ )
{
fact = fact*i;
}
document.getElementById ("num").innerHTML = "Factorial of "+n+" is : "+ fact;
}
</script>
</html>