An Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.
To find Armstrong numbers in Java, you can use a loop to iterate over a range of numbers and check if each number is an Armstrong number.
Here's an example of how to find Armstrong numbers in Java:
for (int i = 100; i <= 999; i++) { int sum = 0; int temp = i; while (temp > 0) { int digit = temp % 10; sum += digit * digit * digit; temp /= 10; } if (sum == i) { System.out.println(i + " is an Armstrong number"); } }
In the above example, a loop is used to iterate over the numbers from 100 to 999. For each number, a variable sum
is used to keep track of the sum of the cubes of the digits. The number is then divided by 10 and the remainder is extracted as the digit. The digit is then cubed and added to the sum. This process is repeated until all the digits have been processed.
Finally, the sum
is compared to the original number. If they are equal, the number is an Armstrong number and a message is printed to the console.
Keep in mind that this example only checks for three-digit Armstrong numbers. To find Armstrong numbers of other lengths, you can modify the range of the loop and the logic for extracting the digits and calculating the sum.