Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 25 July 2017

PHP programs to find Factorial of a number

PHP programs for Factorial of a number:- 

Today i am going to tell you that how can we find Factorial of a number by the simplest method:-
Logic-
1:- If we want to calculate the factorial of 5 then we should initialised a variable which store 5 or stor any digit for which we find factorial.
                       5! = 5x4x3x2x1 // $num = 5;
2. we need another variable which holds the updated multiplied number with the .
                                        $factorial = 1;

Factorial of a number using while loop

<?php
$factorial = 1 ;
$num = 5 ;
while($num > 0)
{
$factorial *=  $num;     // $factorial = $factorial * $num;
$num = $num-1;     // $num--;
}
echo "Factorial of 5 is : ".$factorial."<br />";
?>


Factorial of a number using FOR loop

<?php
$factorial = 1;
$num = 5 ;
for($num; $num>0; $num--)
{
$factorial *= $num;
}
echo "Factorial of 5 is : ".$factorial."<br/>"
?>