Factorial Program in JavaScript
Posted by Superadmin on May 02 2023 13:38:30

Factorial Program in JavaScript

By Priya PedamkarPriya Pedamkar
  

factorial program in javascript

Introduction to Factorial Program in JavaScript

Factorial is a significant operation available in mathematics. It can be used in various types of applications. It is often a requirement to calculate the factorial on browsers. It is very much possible that we can calculate the factorial of the number using the JavaScript. JavaScript will allow us to calculate the factorial of any number at runtime. There is no restriction on the size of the number. It is also necessary that we write efficient code to find out the factorial. In this factorial program in javaScript article, we will see how to find out the factorial of the given number using JavaScript.

Factorial Logic in JavaScript

How to Find the Factorial Program in JavaScript?

Let’s see examples of finding out the factorial in JavaScript using the methods we discussed above.

Example #1

Before we write down the actual JavaScript code we should have something like the webpage to display and interact with. We will design a simple HTML webpage that will have an input box as the number and will display the factorial of the entered number on the same page.

Code:

<!DOCTYPE html>
<html>
<head>
<title> Factorial of Number </title>
</head>
<body>
<h1> Factorial of Number using JavaScript </h1>
<h2> Enter the valid number...! </h2>
<input type = "number" min = "1" id = "num">
<button type = "button" onclick = "submit()" > Submit </button>
<h3> Factorial: <span id = "answer"> </span> </h3>
<script>
// This function will get value from the user and pass it to Factorial function
function submit()
{
var num = parseInt( document.getElementById("num").value);
if( isNaN(num) )
{
alert( " Enter valid number " );
return;
}
document.getElementById("answer").innerHTML = num;
}
</script>
</body>
</html>

Output:

Factorial Program in JavaScript eg1

So we have a simple page with an input box and submit button, upon submitting or on click the function submit() will be called. The function will parse the entered number and display it on the same page.

Let’s modify the same function to calculate the factorial of a number. The submit() function will parse the entered number and will pass it to another function calcFact() which will actually calculate the factorial of a number.

Example #2

By traversing the number in the forward direction starting from 1.

Code:

<!DOCTYPE html>
<html>
<head>
<title> Factorial of Number </title>
</head>
<body>
<h1> Factorial of Number using JavaScript </h1>
<h2> Enter the valid number...! </h2>
<input type = "number" min="1" id = "num">
<button type = "button" onclick = "submit()"> Submit </button>
<h3> Factorial: <span id = "answer"> </span> </h3>
<script>
// This function will get value from user and pass it to Factorial function
function submit()
{
var num = parseInt( document.getElementById("num").value);
if( isNaN(num) )
{
alert(" Enter valid number ");
return;
}
var factorial = calcFact(num);
document.getElementById("answer").innerHTML = factorial;
}
function calcFact( num )
{
var i;
var fact = 1;
for( i = 1; i <= num; i++ )
{
fact = fact * i;
}
return fact;
}
</script>
</body>
</html>

The function calcFact() will calculate the factorial by traversing the number from 1 to number itself. The function receives an argument as num which is then entered number by the user and performs the operation on it. The variable fact is initialized to 1 and then it is multiplied with all the numbers between 1 and entered a number.

Output:

Factorial Program in JavaScript eg2

Example #3

The same function calcFact() can be written in this way, By traversing the number in reverse direction starting from number itself to 1.

Code:

function calcFact( num )
{
var i;
var fact = 1;
for( i = num; i >= 1; i-- )
{
fact = fact * i;
}
return fact;
}

Output:

Example 3

Here the variable i is initialized to number itself and is traversed by performing i–operation i.e. in reverse direction. This function will give the same result similar to the previous one.

Example #4

By using the recursive function.

Code:

function calcFact( num )
{
if(num == 1)
{
return num;
}
else
{
return num * calcFact( num-1 );
}
}

The function calFact() will call itself until it reaches to value 1, but while passing the value it will pass it decreasing by 1 every time. It will go on multiplying with the original result till it reaches value 1. The final multiplying result will be the factorial of a number itself.

 Output:

Example 4

Conclusion

So, Factorial being an important operation in mathematics, it is easy to implement it in the JavaScript. JavaScript allows us to perform dynamic operations such as getting the number and performing operations on it at runtime. We have seen the different ways by which we can calculate the factorial in JavaScript.

Recommended Articles

This is a guide to Factorial Program in JavaScript. Here we discuss the logic and various methods to find the factorial program in javascript with its code implementation. You may also look at the following articles to learn more –