Get Factorial of a Number using jQuery
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

The value of 0! is 1, according to the convention for an empty product.
Read full about factorial here on wikipedia.
We will use For loop for getting factorial of a number, so we just have to decrease value by one and multiply with previous holding temporary value, We have to initiate temporary value with 1.
The Following will be code:
<input type="text" name="factroial4" id="factroial4" />
<br/>
<button type="button" name="button" onclick="GetFactorial();" />
Calculate Factorial
</button>
<br/>
<div id="showresulthere"></div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
// function defination
function GetFactorial()
{
var value = $("#factroial4").val();
tempvalue = 1;
for ( i=value ; i>0 ; i-- )
{
tempvalue *= i ;
// this is same as the tempvalue = tempvalue * i ;
}
$("#showresulthere").html("Factorial of "+value+" is :"+tempvalue);
}
// function defination
</script>
The value of 0! is 1, according to the convention for an empty product.
Read full about factorial here on wikipedia.
We will use For loop for getting factorial of a number, so we just have to decrease value by one and multiply with previous holding temporary value, We have to initiate temporary value with 1.
The Following will be code:
<input type="text" name="factroial4" id="factroial4" />
<br/>
<button type="button" name="button" onclick="GetFactorial();" />
Calculate Factorial
</button>
<br/>
<div id="showresulthere"></div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
// function defination
function GetFactorial()
{
var value = $("#factroial4").val();
tempvalue = 1;
for ( i=value ; i>0 ; i-- )
{
tempvalue *= i ;
// this is same as the tempvalue = tempvalue * i ;
}
$("#showresulthere").html("Factorial of "+value+" is :"+tempvalue);
}
// function defination
</script>
Comments
Post a Comment