English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا البرنامج، ستعلم كيفية استخدام الدوال في Java لعرض جميع الأرقام الأمستونية بين فاصلين مسبقين (المنخفض والعالي)
لإيجاد جميع الأرقام الأمستونية بين اثنين من الأرقام الكاملة، سيتم إنشاء دالة checkArmstrong(). هذه الدالةتحقق من ما إذا كان الرقم 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; // Bit count calculation while (originalNumber != 0) { originalNumber /= 10; ++digits; } originalNumber = num; // The result includes 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 takes a parameter num and returns a boolean value.
If the number is an Armstrong number, return true. If not, return false.
Print output numbers on the main() function based on the return value.