A positive integer is called an Armstrong number of order n if
abcd... = an + bn + cn + dn + ...
In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
Example 1: Check Armstrong Number for 3 digit number
public class Armstrong { public static void main(String[] args) { int number = 371, originalNumber, remainder, result = 0; originalNumber = number; while (originalNumber != 0) { remainder = originalNumber % 10; result += Math.pow(remainder, 3); originalNumber /= 10; } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Output
371 is an Armstrong number.
Example 2: Check Armstrong number for n digits
public class Armstrong { public static void main(String[] args) { int number = 1634, originalNumber, remainder, result = 0, n = 0; originalNumber = number; for (;originalNumber != 0; originalNumber /= 10, ++n); originalNumber = number; for (;originalNumber != 0; originalNumber /= 10) { remainder = originalNumber % 10; result += Math.pow(remainder, n); } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); } }
Output
1634 is an Armstrong number.