Write a program in php to find the factorial of a number

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 - 

Factorial of the number 8 is 40320


No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...