In this post, you will learn Armstrong number & its uses in PHP. Armstrong number is a number that is equal to the sum of cubes of its digits.
Armstrong number is a number that is equal to the sum of cubes of its digits
Code Sinnpet of Armstrong Number in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $num=150; $sum=0; $temp=$num; while($temp!=0) { $rem=$temp%10; $sum=$sum+$rem*$rem*$rem; $temp=$temp/10; } if($num==$sum) { echo "Armstrong number"; } else { echo "This is not an Armstrong number"; } ?> |
Output – This is not an Armstrong number
Some examples of Armstrong Number
0, 1, 153, 370, 371, and 407 are the Armstrong numbers.
Test 371. Three digits. 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371. Armstrong number.
Test 1634. Four digits. 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634. Armstrong number.
There are no 2-digit Armstrong numbers.
There are four 3-digit Armstrong numbers.
There are two 4-digit Armstrong numbers