Home >>PHP Programs >PHP Program to find armstrong number

PHP Program to find armstrong number

PHP Program to find armstrong number

In this example, we will create a PHP program to find if the given number is an Armstrong number or not.

A number is known as Armstrong number if its values is equal to the sum of the cubes of its digits.

Program to Find Armstrong number in PHP Example 1

<?php  
$num=407;  
$total=0;  
$x=$num;  
while($x!=0)  
{  
$rem=$x%10;  
$total=$total+$rem*$rem*$rem;  
$x=$x/10;  
}  
if($num==$total)  
{  
echo "Yes it is an Armstrong number";  
}  
else  
{  
echo "No it is not an armstrong number";  
}  

?> 

Output:-
Yes it is an Armstrong number
Program to Find Armstrong number in PHP Example 2

<?php  
$num=123;  
$total=0;  
$x=$num;  
while($x!=0)  
{  
$rem=$x%10;  
$total=$total+$rem*$rem*$rem;  
$x=$x/10;  
}  
if($num==$total)  
{  
echo "Yes it is an Armstrong number";  
}  
else  
{  
echo "No it is not an armstrong number";  
}  

?> 

Output:-
No it is not an armstrong number