Useful programs

Php Armstrong number

  • 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. ?>  

Output:

null

PHP Armstrong number 1

Look at the above snapshot, the output displays that 407 is an Armstrong number.

Design a site like this with WordPress.com
Get started