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



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.

Updated on: 2020-07-02T07:08:42+05:30

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements