Calculate Factorial of a number using JavaScript
Here you will learn to calculate Factorial of a number using JavaScript using HTML language.
Factorial is a mathematical operation that multiplies a given number by every number below it.
For example:
Factorial of 6 is 6 x 5 x 4 x 3 x 2 x 1 = 720
Program code to Calculate Factorial of a number using JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!-- Calculate Factorial of a Number Using JavaScript --> <!DOCTYPE html> <html> <head> <script type="text/javascript"> function calculateFactorial() { var number = document.getElementById("number").value; var result = 1; for (var i = 1; i <= number; i++) result = result * i; document.getElementById("factResult").innerHTML = "Factorial of " + number + " is " + result; } </script> </head> <body> <h1>Calculate Factorial of a Number Using JavaScript</h1> <input type="number" id="number" value="5" /> <button type="button" onclick="calculateFactorial()">Calculate Factorial</button> <p id="factResult"></p> </body> </html> |
Output
Check out our other JavaScript examples