Armstrong Number

Armstrong Number

An Armstrong number is the one that equals the sum of its digits raised to the power of the number of digits in that number which is to be checked. To be more clear, let the number be n and the number of digits be x. We represent the number as nxnx-1nx-2...n3n2n1 where n1, n2, n3...nx are single digits 0-9. n is an Armstrong number if
n1x + n2x + n3x + nx-1x + nxx = n
153, 371, 9474 and 54748 are few Armstrong numbers.
153 = 13 + 53 + 33
371 = 33 +73 +13
9474 = 94 + 44 +74 + 44
54748 = 55 + 45 + 75 + 45 + 85


   class Armstrong
   {
     public static void main(String args[])
     {
       int num = Integer.parseInt(args[0]);
       int n = num; //use to check at last time
       int check=0,remainder;
       while(num > 0)
       {
         remainder = num % 10;
         check = check + (int)Math.pow(remainder,3);
         num = num / 10;
       }
       if(check == n)
         System.out.println(n+" is an Armstrong Number");
       else
         System.out.println(n+" is not a Armstrong Number");
     }
   } 
Armstrong Number Armstrong Number Reviewed by Abdul hanan on 02:40:00 Rating: 5
Powered by Blogger.