# Java Program to Display Armstrong Numbers Between Intervals Using Function

## Introduction

In number theory, an Armstrong number (also known as a narcissistic, pluperfect, or pluperfect digital invariant) is a number that is the sum of its own digits each raised to the power of the number of digits. In this blog post, we will explore the concept of Armstrong numbers, understand the algorithm to identify them, and implement a Java program to display Armstrong numbers within a given interval using a function. The post will include a step-by-step guide to the algorithm, the Java program’s logic, and the program’s output.

## Algorithm for Identifying Armstrong Numbers

The algorithm to identify Armstrong numbers within a given interval involves iterating through the numbers in that interval, calculating the sum of the -th power of each digit, and checking if the sum is equal to the original number. The steps can be summarized as follows:

1. Input Range: Obtain the lower and upper limits of the interval.
2. Iterate Through Numbers: For each number in the interval, check if it is an Armstrong number.
3. Calculate Sum of Powers: Calculate the sum of -th powers of each digit.
4. Check for Armstrong Number: Compare the sum with the original number.
5. Display Results: Print the Armstrong numbers within the given interval.

## Java Program for Displaying Armstrong Numbers

Now, let’s implement the algorithm in a Java program:

import java.util.Scanner;

public class ArmstrongNumbers {
// Function to calculate the order of a number
static int order(int num) {
int count = 0;
while (num != 0) {
count++;
num /= 10;
}
return count;
}

// Function to check if a number is an Armstrong number
static boolean isArmstrong(int num) {
int order = order(num);
int temp = num;
int sum = 0;

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

return sum == num;
}

// Function to display Armstrong numbers in a given range
static void displayArmstrongNumbers(int lower, int upper) {
System.out.println("Armstrong numbers between " + lower + " and " + upper + ":");
for (int i = lower; i <= upper; i++) {
if (isArmstrong(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input range from the user
System.out.print("Enter the lower limit of the interval: ");
int lowerLimit = scanner.nextInt();
System.out.print("Enter the upper limit of the interval: ");
int upperLimit = scanner.nextInt();

// Display Armstrong numbers in the given range
displayArmstrongNumbers(lowerLimit, upperLimit);
}
}


## Output:

For example, if the user enters the interval [100, 1000], the output will be:

Enter the lower limit of the interval: 100
Enter the upper limit of the interval: 1000
Armstrong numbers between 100 and 1000:
153 370 371 407


## Conclusion

Identifying Armstrong numbers is an interesting problem in number theory, and implementing a program to find them adds a practical aspect to the concept. This blog post has provided an explanation of Armstrong numbers, the algorithm to identify them, and a Java program to display Armstrong numbers within a given interval. Feel free to experiment with different intervals and observe how the program correctly identifies Armstrong numbers based on the specified conditions. Happy coding!