Factorial program in JavaScript
In this post, you will learn how to find the factorial of a number using the JavaScript programming language.
The factorial is normally used in different areas of mathematics, including algebra, mathematical analysis, and Combinations and Permutations. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as-
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
Example of factorial
Suppose we want to get a factorial of 7, then the factorial will be.
7! = 7 X 6 X 5 X 4 X 3 X 2 X 1
5040
Factorial program in JavaScript using for loop
In the given JavaScript program, we have used the for loop to iterate through all numbers between 1 and the entered number, and getting the factorial.
<!DOCTYPE html>
<html>
<head>
</head>
<body style="text-align: center; font-size: 20px;">
<h1>JavaScript Factorial Program</h1>
Enter a number: <input id = "num">
<br><br>
<button onclick="getFact()"> Find Factorial </button>
<p id="res"></p>
<script>
function fact(num)
{
if (num == 0) {
return 1;
}
else {
return num * fact( num - 1 );
}
}
function getFact()
{
var n = document.getElementById("num").value;
var fct = fact(n);
document.getElementById("res").innerHTML="Factorial of the number " + n + " : " + fct ;
}
</script>
</body>
</html>
Output of the above code: Factorial program in JavaScript using recursion method
Here is the JavaScript program to find the factorial of a number using the recursion function. When a user enters a number as input, it passes to the function fact(). If the number equals 0, it returns 1. The factorial of a number less than equals to 0 does not exist. If the input is greater than 1, the function calls recursively and finds the factorial of a number.
<!DOCTYPE html>
<html>
<head>
</head>
<body style="text-align: center; font-size: 20px;">
<h1>JavaScript Factorial Program</h1>
Enter a number: <input id = "num">
<br><br>
<button onclick="getFact()"> Find Factorial </button>
<p id="result"></p>
<script>
function fact(num)
{
if (num == 0) {
return 1;
}
else {
return num * fact( num - 1 );
}
}
function getFact()
{
var num = document.getElementById("num").value;
var fct = fact(num);
document.getElementById("result").innerHTML="Factorial of the number " + num + " : " + fct ;
}
</script>
</body>
</html>
Output of the above code: Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh