The factorial of a number represents the multiplication of all positive integers in descending order. The factorial of a non-negative integer \( n \) is symbolized by \( n! \). For instance -
4! = 4 3 2 * 1 = 24
5! = 5 4 3 2 1 = 120
In this context, 4! is articulated as "four factorial"; it is alternatively referred to as "four bang" or "four shriek".
In this article, we will explore how to compute the factorial of a number utilizing JavaScript. We will demonstrate two distinct methods for determining the factorial. The first method employs an iterative approach, while the second method utilizes a recursive approach.
Using Iterative approach
In this section, we are looping through a series of numbers to calculate the factorial of a specified integer. This method utilizes less memory compared to the recursive approach. However, it results in a longer code structure than that of the recursive version.
Let's see an example of the same.
Example
In this illustration, there exists a text input field designated for a numerical entry along with a button that computes the factorial of the input number. To obtain the factorial of the specified number, one must first input a number into the provided text field. Subsequently, it is necessary to click the button labeled Factorial to receive the computed result.
In the event that a negative value is inputted, the program will compute the factorial of 0, resulting in a value of 1.
<!DOCTYPE html>
<html>
<head>
</head>
<body style = "text-align: center; font-size: 20px;">
<h1> Welcome to the logic-practice.com </h1>
Enter a number: <input id = "num">
<br><br>
<button onclick = "fact()"> Factorial </button>
<p id = "res"></p>
<script>
function fact(){
var i, num, f;
f = 1;
num = document.getElementById("num").value;
for(i = 1; i <= num; i++)
{
f = f * i;
}
i = i - 1;
document.getElementById("res").innerHTML = "The factorial of the number " + i + " is: " + f ;
}
</script>
</body>
</html>
Output
Upon running the code provided above, the resulting output will be -
Upon inputting the number and pressing the designated button, the resulting output will be -
Next, we will explore the process of computing the factorial using a recursive approach in JavaScript.
Using Recursive approach
In this method, we employ recursion to determine the factorial of a given number. This involves repeatedly invoking the same function to compute the factorial. By utilizing recursion, we can achieve a more concise code compared to the iterative method.
Next, we will explore an illustration of calculating the factorial of a number through the use of recursion in JavaScript.
Example
In this context, we have a function named fact that takes a single argument, num. This argument represents the number for which we aim to compute the factorial. The function yields a return value of 1 when num is equal to 0.
In the resulting output, there will be a text input area designated for numerical entries and a button that calculates the factorial of the specified number. To determine the factorial of a particular number, we need to input a value into the provided text field. Following this, we must press the button labeled "Factorial" in order to receive the computed result.
<!DOCTYPE html>
<html>
<head>
</head>
<body style = "text-align: center; font-size: 20px;">
<h1> Welcome to the logic-practice.com </h1>
Enter a number: <input id = "number">
<br><br>
<button onclick = "fact1()"> Factorial </button>
<p id = "res"></p>
<script>
function fact(num)
{
if (num == 0) {
return 1;
}
else {
return num * fact( num - 1 );
}
}
function fact1()
{
var num = document.getElementById("number").value;
var f = fact(num);
document.getElementById("res").innerHTML="The factorial of the number " + num + " is: " + f ;
}
</script>
</body>
</html>
Output
Upon running the code provided above, the resulting output will be -
Upon inputting the number and pressing the specified button, the result will be -