Write a Java Program to Display Armstrong Numbers Between Intervals Using Function

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits in the number.

For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

In this Java program, we will use a function to display all Armstrong numbers between two given intervals.


First, we will create a function called “isArmstrong” that takes an integer as input and returns true if the number is an Armstrong number, and false otherwise.

To check if a number is an Armstrong number, we need to count the number of digits in the number, raise each digit to the power of the number of digits, and then add them together.

If the result is equal to the original number, then it is an Armstrong number.

Here’s the code for the “isArmstrong” function:

public static boolean isArmstrong(int num) {
    int sum = 0;
    int temp = num;
    int digits = String.valueOf(num).length();

    while (temp != 0) {
        int digit = temp % 10;
        sum += Math.pow(digit, digits);
        temp /= 10;
    }

    return sum == num;
}

Next, we will create a main function that takes two integers as input (the starting and ending intervals) and loops through all the numbers between the two intervals.

For each number, we will call the “isArmstrong” function and print the number if it is an Armstrong number.

Here’s the code for the main function:

public static void main(String[] args) {
    int start = 100;
    int end = 999;

    for (int i = start; i <= end; i++) {
        if (isArmstrong(i)) {
            System.out.println(i);
        }
    }
}

In this example, we have set the starting interval to 100 and the ending interval to 999.

You can change these values to any other intervals that you want to check for Armstrong numbers.

When you run the program, it will print all Armstrong numbers between the given intervals.

For example, if you set the intervals to be 100 and 999, the program will print the following output:

153
370
371
407

This is because these are the only Armstrong numbers between 100 and 999.

You can modify the program to take the intervals as user input by using the Scanner class in Java.

Here’s the modified main function:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the starting interval:");
    int start = sc.nextInt();
    System.out.println("Enter the ending interval:");
    int end = sc.nextInt();

    for (int i = start; i <= end; i++) {
        if (isArmstrong(i)) {
            System.out.println(i);
        }
    }
}

This version of the program prompts the user to enter the starting and ending intervals, and then uses those values to loop through all the numbers between the intervals and check for Armstrong numbers.


Overall, this program demonstrates how to use functions to check for Armstrong numbers between two intervals in Java.

It can be easily modified to take different intervals as input or to print the Armstrong numbers in a different format.