Armstrong number in PHP

An Armstrong number is a number such that the sum of its digits raised to the third power is equal to the number itself. For example, 371 is an armstrong number, since ! 3**3 + 7**3 + 1**3 = 371.


Here is code...
<?php
 $arm=371;
 $num=$arm;
 $res=0;
 while($num!=0)
 {
 $a=$num%10;
 $res=$res+($a*$a*$a);
 $num=$num/10;
 }
 if($arm==$res)
 {
 echo $arm." is an armstrong.";
 }
 else
 {
 echo $arm." is not armstrong.";
 }

?>

Labels: