Factorial program 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-1.Write a javascript program to find and display factorial of number.
(Using for loop) (suppose number is 5.)
<!DOCTYPE html>
<html>
<body>
<h2>Factorial OF Number</h2>
<script type="text/javascript">
var i,n=5,fact=1;
for(i=1;n>=i;n--)
{
fact=fact*n;
}
document.write("Factorial of Number="+fact);
</script>
</body>
</html
Output-
Factorial OF Number
Factorial of Number=120- 2.Find Factorial of any number accept form user.(Using for loop)
<!DOCTYPE html>
<html>
<body>
<h2>Factorial OF Number</h2>
<script type="text/javascript">
var i,n,fact=1;
n=prompt("Enter number to find Factorial");
for(i=1;n>=i;n--)
{
fact=fact*n;
}
document.write("Factorial of Number="+fact);
</script>
</body>
</html>
Output-
Suppose user enter number is 6.
Factorial OF Number
Factorial of Number=720- 3.Example-Factorial Using While Loop
<!DOCTYPE html>
<html>
<body>
<h2>Factorial OF Number</h2>
<script type="text/javascript">
var i,n,fact=1;
i=1;
n=prompt("Enter number to find Factorial");
while(n>=i)
{
fact=fact*n;
n--;
}
document.write("Factorial of Number="+fact);
</script>
</body>
</html>
Output-
Suppose user enter number is 5.
Factorial OF Number
Please Comment and Share. ConversionConversion EmoticonEmoticon