$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 ";
}
?>
Listing all strong numbers
Without taking user input we can list all strong number over a range. We can find out all strong numbers less than 100000.
<?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> ";
}
}
?>