In the following example, we will check whether the number 19 is an Armstrong number or not.
Example
PHP Compiler
<?php
$num=19;
$copyNum=$num;
$digits=0;
$remainder=0;
$total=0;
// find number of digits in num variablewhile($copyNum!=0)
{
$digits++;
$copyNum=floor($copyNum/10);
}
$copyNum=$num;
// slice the numbers from last digitswhile($copyNum!=0)
{
$remainder=$copyNum%10;
$total+=pow($remainder, $digits);
$copyNum=floor($copyNum/10);
}
// resultif($num==$total)
echo"$num is an armstrong number";
elseecho"$num is not an armstrong number";
?>
Output
19 is not an armstrong number
Armstrong Numbers between the Given Range
In the following example, we will find all the Armstrong numbers between 1 and 200.
Example
PHP Compiler
<?php
$start=1;
$end=200;
$flag=0;
for($start=$start; $start<=$end; $start++)
{
$copyNum=$start;
$total=0;
$digits=0;
$remainder=0;
// find the number of digits in start variablewhile($copyNum!=0)
{
$digits++;
$copyNum=floor($copyNum/10);
}
$copyNum=$start;
//slice the start variable from last digitwhile($copyNum!=0)
{
$remainder=$copyNum%10;
$total+=pow($remainder, $digits);
$copyNum=floor($copyNum/10);
}
// resultif(($start==$total) && ($start!=0))
{
if($flag==0)
{
echo"Armstrong numbers between $start and $end:<br>";
$flag=1;
}
echo"$start ";
}
}
if($flag==0)
echo"There is no armstrong numbers between the given range.";
?>
Output
Armstrong numbers between 1 and 200:
1 2 3 4 5 6 7 8 9 153
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on PHP with examples for quick and easy learning.
We are working to cover every Single Concept in PHP.