Armstrong Number Program in Java

 

Program to find that given number is Armstrong or not.

/**
 * This program is used to find that given number is Armstrong or not.
 * @author W3spoint
 */
public class ArmstrongNumber {
      /**
       *This method is used to find that given number is Armstrong or not
       *@param num
       */
      static void armstrong(int num){
            int newNum = 0, remainder, temp;
            temp = num;
            //find sum of all digit's cube of the number.
            while(temp != 0){
                  remainder= temp % 10;
                  newNum = newNum + remainder*remainder*remainder;
                  temp = temp/10;
            }
            //Check if sum of all digit's cube of the number is
            //equal to the given number or not.
            if(newNum == num){
                  System.out.println(num +" is armstrong.");
            }else{
                  System.out.println(num +" is not armstrong.");
            }
      }     
 
      public static void main(String args[]){
            //method call
            armstrong(407);
      }
}

Output:

407 is armstrong.

 

Content Protection by DMCA.com
Please Share