English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce programme, vous apprendrez à utiliser les fonctions en Java pour afficher tous les nombres d'Armstrong entre deux intervalles (bas et haut) donnés
Pour trouver tous les nombres d'Armstrong entre deux entiers, une fonction checkArmstrong() sera créée. Cette fonctionVérification si un nombre est un nombre d'Armstrong。
public class Armstrong { public static void main(String[] args) { int low = 999, high = 99999; for(int number = low + 1; number < high; ++number) { if (checkArmstrong(number)) System.out.print(number + " "); } } public static boolean checkArmstrong(int num) { int digits = 0; int result = 0; int originalNumber = num; //Digit Calculation while (originalNumber != 0) { originalNumber /= 10; ++digits; } originalNumber = num; //The result contains the sum of the n-th powers of its digits while (originalNumber != 0) { int remainder = originalNumber % 10; result += Math.pow(remainder, digits); originalNumber /= 10; } if (result == num) return true; return false; } }
When running the program, the output is:
1634 8208 9474 54748 92727 93084
In the above program, we created a function named checkArmstrong() that accepts a parameter num and returns a boolean value.
If the number is an Armstrong number, return true. Otherwise, return false.
Print output numbers on the main() function based on the return value.