---
title: Display armstrong numbers between intervals using function
url: https://docs.vultr.com/java/examples/display-armstrong-numbers-between-intervals-using-function
publish_date: 2024-11-29T21:00:00.086573Z
last_updated: 2024-11-29T21:00:00.086573Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/1175/2692c123-70bb-4330-bcd3-4b81b8ed596c.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Display Armstrong Numbers Between Intervals Using Function

## Introduction
An Armstrong number (also known as a narcissistic number) is a number that equals the sum of its own digits each raised to the power of the number of digits. For instance, 153 is an Armstrong number because \(1^3 + 5^3 + 3^3 = 153\). Exploring Armstrong numbers between two intervals can be a common task in introductory Java programming courses, providing a great way to learn about loops, conditionals, and functions.

In this article, you will learn how to create a Java program that displays Armstrong numbers between two intervals. The explanations, followed by code examples, will guide you through writing a function that checks for Armstrong numbers, and then using that function to find and display them within a specified range.

## Defining the Armstrong Number Check Function

### Outline of the Function
1. Define a function that takes an integer as input.
1. Compute the number of digits in the integer.
1. Sum the powers of each digit according to the number of digits.
1. Compare the sum to the original number and return a boolean value.

### Implementation of the Function
1. Utilize Java's mathematical operations to break down the number into digits.
1. Calculate the sum of the digits each raised to the power of the total number of digits.
1. Return `true` if the sum equals the original number, otherwise return `false`.

    ```java
    public static boolean isArmstrong(int number) {
        int originalNumber, remainder, result = 0, n = 0;
        originalNumber = number;

        for (;originalNumber != 0; originalNumber /= 10, ++n);

        originalNumber = number;

        for (;originalNumber != 0; originalNumber /= 10) {
            remainder = originalNumber % 10;
            result += Math.pow(remainder, n);
        }

        return (result == number);
    }
    ```

    In this code:
    * The first loop counts the digits in the number.
    * The second loop calculates the sum of each digit raised to the power of the number of digits.

## Implementing the Main Method to Display Armstrong Numbers

### Setting Up the Main Method
1. Read the lower and upper limits for the interval from the user.
1. Loop through the interval using a `for` loop.
1. Use the function `isArmstrong()` to check each number, and print it if it is an Armstrong number.

### Code for Main Method
1. Create a Scanner object to capture user input.
1. Promote good user interaction by providing clear prompts.
1. Iterate through the desired range, and use the function to check for Armstrong numbers.

    ```java
    import java.util.Scanner;

    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int low, high;

            System.out.print("Enter the lower bound: ");
            low = scanner.nextInt();
            System.out.print("Enter the upper bound: ");
            high = scanner.nextInt();

            System.out.println("Armstrong numbers between " + low + " and " + high + " are: ");
            for (int i = low; i <= high; i++) {
                if (isArmstrong(i)) {
                    System.out.println(i);
                }
            }
        }
    }
    ```

    * The `Scanner` class is used for capturing user input.
    * The `for` loop runs through each number in the user-specified range.
    * The `isArmstrong()` method is called to check each number.

## Conclusion
By following the steps outlined above, you now have a Java program capable of identifying and displaying Armstrong numbers within a specified interval. Such tasks enhance understanding of functions, loops, and basic number manipulation in Java. Implementing these concepts in practical applications like finding Armstrong numbers provides valuable hands-on experience in programming basics. This knowledge applies across various scenarios, making your programming journey easier and more efficient.