English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn how to display all Armstrong numbers between two given intervals (low and high) in Java.
A positive integer is called an n-order Armstrong number if
abcd... = an + bn + cn + dn + ...
For3digit Armstrong number, the sum of the cubes of each digit equals the number itself. For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153is an Armstrong number.
This program is based onHow to check if an integer is an Armstrong numberconcept.
public class Armstrong { public static void main(String[] args) { int low = 999, high = 99999; for(int number = low + 1; number < high; ++number) { int digits = 0; int result = 0; int originalNumber = number; //Digit Calculation while (originalNumber != 0) { originalNumber /= 10; ++digits; } originalNumber = number; //The result is the sum of the nth powers of its digits while (originalNumber != 0) { int remainder = originalNumber % 10; result += Math.pow(remainder, digits); originalNumber /= 10; } if (result == number) System.out.print(number + ""); } } }
When running the program, the output is:
1634 8208 9474 54748 92727 93084
In the above program, each number between the given high and low intervals was checked.
After each check, digits and result will be restored to 0.