Java Armstrong Number Program

 

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* 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);
}
}
/** * 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); } }
/**
 * 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
407 is armstrong.
407 is armstrong.
407 is armstrong.