This is the second solution to check the number for Armstrong. This solution contains the core logic to extract each digit from the given number, in parallel it will compute the sum of the cubes of each digit. The Armstrong number is say for a given number 153, evaluate the powers of cubes from any side as power (1 * 3) + power (5 * 3) + power (3 *3) , the sum of all powers must be equal to the given number 153.
public class ArmstrongNumbersCoreLogic {
public static void main(String[] args) {
int sumCheck=0,remValue;
System.out.println("The Armstrong numbers between 1 to 1000");
for (int numValue=1;numValue<=1000;numValue++) {
/* Find the reminder after each iteration till the number equals to 0.
Calculate the sum of cubes of all digits of the original number */
int storeNum=numValue;
while (storeNum !=0) {
remValue=storeNum % 10;
sumCheck=sumCheck+(remValue * remValue * remValue);
storeNum=storeNum/10;
}
//Compare the sumCheck with actual number
if (sumCheck == numValue)
System.out.println(numValue+" is Armstrong number");
//Reset sumCheck to 0 for next iteration
sumCheck=0;
}
}
}
Output:
The Armstrong numbers between 1 to 1000
1 is Armstrong number
153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number

Leave a comment