English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In diesem Programm lernen Sie, wie man in Java alle Armstrong-Zahlen zwischen zwei gegebenen Intervallen (niedrig und hoch) anzeigt.
Eine positive Ganzzahl wird eine n-stufige Armstrong-Zahl genannt, wenn
abcd... = an + bn + cn + dn + ...
Für3Stellen der Armstrong-Zahlen, die Summe der Kubikzahlen jedes Ziffern gleich dem Ziffern selbst ist. Zum Beispiel:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153ist eine Armstrong-Zahl.
Dieses Programm basiert aufWie überprüft man, ob eine Ganzzahl eine Armstrong-Zahl istKonzept.
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 contains 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.