You are here: Home / Topics / PHP Program to check Armstrong number

PHP Program to check Armstrong number

Filed under: PHP on 2022-11-16 13:39:41

The below program checks for an Armstrong number using a loop. The PHP echo statement is used to output the result on the screen. A number is called as an Armstrong number if the sum of the cubes of its digits is equal to the number itself.

Armstrong Number:

abc = (a*a*a) + (b*b*b) + (c*c*c)

Example: 0, 1, 153, 371, 407, 471, etc.

Example

<!DOCTYPE html>
<html>
<body>

<?php  

$n=371;  
$sum=0;  
$i=$n;

while($i!=0)  
{  
$x=$i%10;  
$sum=$sum+$x*$x*$x;  
$i=$i/10;  
}

if($n==$sum)  
{  
echo "$n is an Armstrong Number.";  
}  

else  
{  
echo "$n is not an Armstrong Number.";  
}  
?> 

</body>
</html>
Output

371 is an Armstrong Number.
 

About Author:
V
Vishal Gupta     View Profile
Hi, I am Vishal Gupta. I like this Website for learning MCQs. This website is good for preparation.