Armstrong Number Java Program

The logic behind an Armstrong number is that, when the sum of cubes of the individual digits of any number is equal to that number, then the number is called as an Armstrong number.
For example :
0 = 0^3+0^3+0^3 is an Armstrong number.
1 = 1^3+1^3+1^3 is also an armsstrong number.
153 = 1^3+5^3+3^3. is also an Armstrong number.
Some of the examples of Armstrong number are 0, 1, 153, 370, 407 etc. 

Armstrong Number checker Java Program:

 /*Write a program to find whether given no. is Armstrong or not.


  Example :

           Input - 153

           Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */

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");

   }

}

Output of the Armstrong Number Java Program


0 comments:

Post a Comment