The factorial of a number n is defined by the product of all the digits from 1 to n (including 1 and n).
For example,
4! = 4*3*2*1 = 24
6! = 6*5*4*3*2*1 = 720
Logic:
Take a number.
Take the descending positive integers.
Multiply them.
Code
<?php
//example to calculate factorial of a number using function
//defining the factorial function
function Factorial_Function($number) {
$input = $number;
$fact=1;
//iterating using for loop
for($i=$input; $i>=1;$i--) {
$fact = $fact * $i;
}
return $fact;
}
//calling the factorial function
$result = Factorial_Function(8);
echo 'Factorial of the number 4 is '.$result;
?>
Output -
No comments:
Post a Comment