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,

5! = 5  \times  4  \times  3  \times  2  \times  1 = 120.  \

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:

Function oriented way:
<?php
// function defination

function GetFactorial($value)
{
$tempvalue = 1;
for ( $i=$value ; $i>0 ;  $i-- )
{
 $tempvalue *=  $i ;
// this is same as the   $tempvalue =  $tempvalue * $i ;
}
return $tempvalue;
}
// function defination


// getting value by calling

echo "<br/>Factorial of 1 is : ".GetFactorial(3);
echo "<br/>Factorial of 2 is : ".GetFactorial(3);
echo "<br/>Factorial of 3 is : ".GetFactorial(3);
echo "<br/>Factorial of 4 is : ".GetFactorial(4);
echo "<br/>Factorial of 5 is : ".GetFactorial(5);
echo "<br/>Factorial of 6 is : ".GetFactorial(6);
echo "<br/>Factorial of 7 is : ".GetFactorial(7);

// getting value by calling
?>

Post a Comment

 
Top