Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
<?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.
Advertisements