Saturday, 17 November 2018

Factorial of a number in PHP

How to print factorial of any number in php.

Example of Factorial

 
5!= 5 * 4 * 3 * 2 * 1 = 120

Factorial of a number in PHP

<?php

$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--) 
{
  $factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";

?>

Output

 
Factorial of 4 is 24

No comments:

Post a Comment

PHP - Functions

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your ...