Display Armstrong Number Between Two Intervals in Java


Write a Java Program to display Armstrong number between two intervals.

Click Here to View the Solution!
public class ArmstrongNumber {
     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;
         // Calculates number of digits.
         while (originalNumber != 0) {
             originalNumber /= 10;
         originalNumber = num;
         // result is  sum of nth power of its digits
         while (originalNumber != 0) {
             int remainder = originalNumber % 10;
             result += Math.pow(remainder, digits);
             originalNumber /= 10;
         if (result == num)
             return true;
         return false;
Click Here to View the Output!
1634 8208 9474 54748 92727 93084 
Click Here to View the Explanation!
  • This program is used for displaying all the Armstrong Numbers between two intervals low and high by using a function in Java.
  • The function used in the program checkArmstrong() is created within the program for finding all the Armstrong numbers between two integers.
  • The user-defined function “checkArmstrong()” takes the variable num as an argument and gives a Boolean value in return.
  • Initially, for the intervals, tow integer variable low and high are set as low = 999 and high = 99999.
  • A for loop holds an initialization (number = low + 1), a conditional expression (number < high) and an expression to update a variable (++number). This loop allows the program to check every number in the interval low and high.
  • In the user-defined checkArmstrong function, the first while loop is used to calculate the number of digits in the originalNumber and stores it in the variable digit.
  • Whereas, the second while loop is used to calculate the remainder of the originalNumber and the result by powering the remainder with the number of digits in every iteration.
  • After the loop exits, both the result and the number are compared. If both are equal, the number is an Armstrong number the function will return true else, false.
  • The checkArmstrong function applied on the number will print the output within the main() method according to the value returned by its defined function.