Php

Armstrong Number

An Armstrong number is the one whose value is equal to the sum of the cubes of its digits.

0, 1, 153, 371, 407, 471, etc are Armstrong numbers.

For example,

  1. 407 = (4*4*4) + (0*0*0) + (7*7*7)  
  2.         = 64 + 0 + 343  
  3. 407 = 407  

Logic:

ADVERTISEMENT

null

ADVERTISEMENT

  • Take the number.
  • Store it in a variable.
  • Take a variable for sum.
  • Divide the number with 10 until quotient is 0.
  • Cube the remainder.
  • Compare sum variable and number variable.

Armstrong number in PHP

null

Below program checks whether 407 is Armstrong or not.

Example:

  1. <?php  
  2. $num=407;  
  3. $total=0;  
  4. $x=$num;  
  5. while($x!=0)  
  6. {  
  7. $rem=$x%10;  
  8. $total=$total+$rem*$rem*$rem;  
  9. $x=$x/10;  
  10. }  
  11. if($num==$total)  
  12. {  
  13. echo “Yes it is an Armstrong number”;  
  14. }  
  15. else  
  16. {  
  17. echo “No it is not an armstrong number”;  
  18. }  
  19. ?>  
Design a site like this with WordPress.com
Get started