Java Armstrong Number Program

 

Program to find whether the given number is Armstrong or not.

/**
 * This program is used to find that given number is Armstrong or not.
 * @author W3schools360
 */
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
                  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.

 

Please follow and like us:
Content Protection by DMCA.com