This post contains a total of 7+ Hand-Picked PHP Armstrong Number Checker & Finder Program Examples with Source Code. All the Armstrong Number Checker & Finder programs are made using PHP Programming Language.
You can use the source code of these programs for educational purpose with credits to the original owner.
Related Posts
Click a Code to Copy it.
1. By Hossein
Made by Hossein. Simple PHP Program to check if a number is Armstrong or not. Enter your own input in $num=153; ( Source )
Armstrong number
<?php
$num=153;
$sum=0;
$temp=$num;
while($temp!=0)
{
$rem=$temp%10;
$sum=$sum+$rem*$rem*$rem;
$temp=$temp/10;
}
if($num==$sum)
{ echo "Armstrong number"; }
else
{ echo "not an armstrong number"; }
?>
2. By Federico Piazzon
Made by Federico Piazzon. PHP Program to get the highest Armstrong number in a given range and also to print out all the Armstrong numbers in that range. ( Source )
Number: 926 Is Armstrong? false Range: 3 - 1924 Armstrong numbers are: - 153 - 370 - 371 - 407
<?php
function isArmstrong($n) {
$digits = str_split($n);
$sum = 0;
foreach ($digits as $digit) {
$sum += $digit**3;
}
return ($sum == $n);
}
$n = rand(0, 1000);
echo "Number: $n<br/>";
echo "Is Armstrong? " . (isArmstrong($n) ? 'true' : 'false');
echo "<br/><br/>";
$min = rand(0, 100);
$max = rand(101, 10000);
$aN = array();
for ($i = $min; $i <= $max; $i++) {
if (isArmstrong($i)) {
$aN[] = $i;
}
}
echo "Range: $min - $max<br/>";
if (!empty($aN)) {
echo "Armstrong numbers are:<br/>";
foreach ($aN as $a) {
echo " - $a<br/>";
}
} else {
echo "No Armstrong numbers found in this range!";
}
?>
3. By Santosh Maruvada
Made by Santosh Maruvada. Prints all the Armstrong number of a given range. ( Source )
0 is an amrstrong number 1 is an amrstrong number 153 is an amrstrong number 370 is an amrstrong number 371 is an amrstrong number 407 is an amrstrong number
<?php
$x=999;
for($i=0;$i<=$x;$i++)
{
$x1=(int)($i/10);
$x2=$i%10;
$y1=($x2**3);
$x3=(int)($x1/10) ;
$x4=$x1%10;
$y2=($x4**3);
$y3=($x3**3);
if(($y1+$y2+$y3)==$i)
{
echo $i.' is an amrstrong number'.'<br/>';
}
}
?>
4. By BjΓΆrn Hallberg
Made by BjΓΆrn Hallberg. All Armstrong numbers between 0 to 10000. ( Source )
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
<?php
// Challenge: Armstrong Numbers
function isArmstrong($num) {
$sum = 0;
$len = strlen($num);
foreach (str_split($num) as $n) {
$sum += pow($n, $len);
}
return $num == $sum;
}
function printArmstrong($from, $to) {
for ($i=$from; $i<=$to; $i++) {
if (isArmstrong($i))
echo "$i ";
}
}
// Test function
printArmstrong(0, 10000);
?>
5. By Mayank Mishra
Made by Mayank Mishra. Change the input number in line $num. ( Source )
No
<?php
$num=406;
$total=0;
$x=$num;
while ($x!=0) {
$rem=$x%10;
$total=$total+$rem*$rem*$rem;
$x=$x/10;
}
if($num==$total) {
echo" Yes";
}else {
echo "No<br>";
}
?>
6. By Shivam Dubey
Made by Shivam Dubey. Find Armstrong number using function in PHP. ( Source )
It is armstrong number:153
<?php
function Armstrong()
{
$a=153;
$b=$a;
$sum=0;
while ($a>0)
{
$r=$a%10;
$sum=$sum+$r*$r*$r;
$a=$a/10;
}
if ($sum==$b)
{
echo "It is armstrong number:$b";
}
else
{
echo "it is not armstrong number:$b";
}
}
Armstrong ();
?>
7. By ramesh Gupta
Made by ramesh Gupta. ( Source )
2345 not armstrong number
<?php
$num = 2345;
$sum=0;
while($num!=0){
$remainder = $num%10;
$sum = $sum + ($remainder * $remainder * $remainder );
$num = $num /10;
}
if($sum == $num ){
echo "is armstrong number";
}else{
echo "not armstrong number";
}
?>
8. By Shubham Sharma
Made by Shubham Sharma. PHP program to find Armstrong number. ( Source )
It is an Armstrong Number
<?php
$a=371;
$c=0;
$save=$a;
while($a>0)
{
$b=$a%10;
$a=(int) ($a/10);
$c=$c+($b*$b*$b);
}
if($c==$save)
echo "It is an Armstrong Number";
else
echo "It is not an armstrong number";
?>