function fact($n){
$factorial=1;
for ($i=1; $i<=$n;$i++){
$factorial=$factorial*$i;
}
return $factorial;
}
Getting all digits of the input number.
$n1=1234; // Sample number
while($n1>0){
$d=$n1%10; // reminder of division
echo $d; // one digit from right
$n1=floor($n1/10); // floor value
}
Using the above two concepts we can find out the sum of factorial of any number. We will create one sticky form to take user input and then find out it is strong number or not.
<?Php
$n1=$_POST['n1'];
echo "<form method=POST action=''>
<input type=text name=n1 value='$n1'>
<input type=submit value=Submit>
</form>";
function fact($n){
$factorial=1;
for ($i=1; $i<=$n;$i++){
$factorial=$factorial*$i;
}
return $factorial;
}
$sum=0;
$n2=$n1;
while($n1>0){
$d=$n1%10; // reminder of division
$sum = $sum + fact($d);
$n1=floor($n1/10); // floor value
}
if($n2==$sum){
echo "$n2 is a Strong Number ";
}else{
echo "$n2 is NOT a Strong Number ";
}
?>
<?Php
function fact($n){
$factorial=1;
for ($i=1; $i<=$n;$i++){
$factorial=$factorial*$i;
}
return $factorial;
}
for($i=10;$i<100000;$i++){
$sum=0;
$n2=$n1=$i;
while($n1>0){
$d=$n1%10; // reminder of division
$sum = $sum + fact($d);
$n1=floor($n1/10); // floor value
}
if($n2==$sum){
echo "$n2 is a Strong Number <br> ";
}
}
?>
Output
145 is a Strong Number
40585 is a Strong Number
Introduction to PHP Factorial of a number Sum of Digits of a number
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.