PHP program to find the first natural number whose factorial can be divided by a number ‘x’

PHPServer Side ProgrammingProgramming

To find the first natural number whose factorial can be divided by a number ‘x’, the code is as follows −

Example

 Live Demo

<?php
function factorial_num($x_val)
{
   $i = 1;
   $fact_num = 4;
   for ($i = 1; $i < $x_val; $i++)
   {
      $fact_num = $fact_num * $i;
      if ($fact_num % $x_val == 0)
         break;
   }
   return $i;
}
$x_val = 16;
print_r("The first natural number whose factorial can be divided by 16 is ");
echo(factorial_num($x_val));
?>

Output

The first natural number whose factorial can be divided by 16 is 4

A function named ‘factorial_num’ computes factorial of a number and checks to see if it is divisible by 16, and if yes, returns that number as output. Outside the function, a number is defined and it is passed as parameter to the function. Relevant output is displayed on the console.

raja
Published on 02-Jul-2020 10:38:28
Advertisements