Factorial of a number in PHP
<?php
$no = 5; /*number to get factorial */
$fact = 1;
for($i=1;$i<=$no;++$i)
{
$fact = $fact*$i;
}
echo "Factorial of $no is ".$fact;
//============ Another way============//
$num = 5; /*number to get factorial */
$facto = 1;
for($k=$num;$k>=1;$k--)
{
$facto = $facto*$k;
}
echo "Factorial of $no is ".$facto;
?>
//============ Another way============//
$no = 5; /*number to get factorial */
$fact = 1;
for($i=1;$i<=$no;++$i)
{
$fact = $fact*$i;
}
echo "Factorial of $no is ".$fact;
//============ Another way============//
$num = 5; /*number to get factorial */
$facto = 1;
for($k=$num;$k>=1;$k--)
{
$facto = $facto*$k;
}
echo "Factorial of $no is ".$facto;
?>
//============ Another way============//
$fact = 1;
// value of 'number'
$number = 5;
echo "Factorial of $number:<br><br>";
//loop till the iterator $i equals to $number
for ($i = 1; $i <= $number; $i++){
//formula to calculate factorial is to
//multiply the iterator $i value with $fact value.
$fact = $fact * $i;
//print $fact to show the factorial pyramid
//or put this line out of this 'for loop' to show only the total value
print $fact . "<br>";
}
Comments
Post a Comment