This blog describes how to find Armstrong Numbers between 1 to 1000 using PHP?. Before we are going to program part, we should know what is Armstrong Number?. So only we can write program for find Armstrong Number in any languages like C, C++, PHP.
what is Armstrong Number?
Armstrong Number is sum of 3 power of each digits equals to given number.
For example:
153 is Armstrong Number.
1^3 + 5^3 + 3 ^3
1 + 125 + 27
153
PHP Program to find Armstrong Numbers:
Consider below example which is find Armstrong Numbers between 1 to 1000.
<?php
for($i = 1; $i <= 1000; $i++) {
$sum = 0;
$qu = $i;
while($qu != 0) {
$remainder = $qu % 10;
$thrice = $remainder * $remainder * $remainder;
$sum += $thrice;
$qu = $qu / 10;
}
if ($sum == $i) {
echo "<p>$i is armstrong number</p>";
}
}
?>
for($i = 1; $i <= 1000; $i++) {
$sum = 0;
$qu = $i;
while($qu != 0) {
$remainder = $qu % 10;
$thrice = $remainder * $remainder * $remainder;
$sum += $thrice;
$qu = $qu / 10;
}
if ($sum == $i) {
echo "<p>$i is armstrong number</p>";
}
}
?>
Now you got Armstrong Numbers between 1 to 1000.
Related Post: