Factorial in javascript.
Factorial of number means multiplication of all positive integer numbers less than or equal to that number.
The factorial of positive integer number n is written as n!.
Example-Suppose we have to find out factorial of number 5,i.e.5!.
5!=5*4*3*2*1 =120
So factorial of number 5!=120.
Example-Factorial of 10 numbers.
<!DOCTYPE html>
<html>
<body>
<h2>Factorial OF Number</h2>
<script type="text/javascript">
var i,n;
i=1;
n=10;
fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
document.write("Factorial of Number"+" "+i+"="+" "+fact+"<br>");
}
</script>
</body>
</html>
Output-
Factorial OF Number
Factorial of Number 1= 1
Factorial of Number 2= 2
Factorial of Number 3= 6
Factorial of Number 4= 24
Factorial of Number 5= 120
Factorial of Number 6= 720
Factorial of Number 7= 5040
Factorial of Number 8= 40320
Factorial of Number 9= 362880
Factorial of Number 10= 3628800
Example-Factorial of numbers accepting from user.(e.g. Suppose User enter 5 number).
<!DOCTYPE html>
<html>
<body>
<h2>Factorial OF Number</h2>
<script type="text/javascript">
var i,n;
i=1;
n=prompt("Enter number to find Factorial");
fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
document.write("Factorial of Number"+" "+i+"="+" "+fact+"<br>");
}
</script>
</body>
</html>
Output-
Factorial OF Number
Factorial of Number 1= 1
Factorial of Number 2= 2
Factorial of Number 3= 6
Factorial of Number 4= 24
Factorial of Number 5= 120
Please Comment and Share. ConversionConversion EmoticonEmoticon