Check Armstrong Numbers Between Two Integers in Java with Code

Armstrong number

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because:

Armstrong Number in Java

In this article, we will write a Java program to check for Armstrong numbers between two given integers. We will implement the logic to traverse through the specified range and identify all Armstrong numbers within that range.

Steps to Implement the Program in Java

  1. Input the Range: Get two integers from the user that define the range.
  2. Check Each Number: Loop through each number in the range and check if it is an Armstrong number.
  3. Count Digits: For each number, count its digits to determine the power for each digit.
  4. Calculate Sum: Calculate the sum of each digit raised to the power of the total number of digits.
  5. Output Results: Print all Armstrong numbers.

Java Code Example

Here’s a complete Java program that implements the above steps.

import java.util.Scanner;

public class ArmstrongNumbersInRange {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Input lower and upper bounds
        System.out.print("Enter lower bound: ");
        int lowerBound = scanner.nextInt();
        System.out.print("Enter upper bound: ");
        int upperBound = scanner.nextInt();

        System.out.println("Armstrong numbers between " + lowerBound + " and " + upperBound + ":");
        
        // Check for Armstrong numbers in the given range
        for (int number = lowerBound; number <= upperBound; number++) {
            if (isArmstrong(number)) {
                System.out.print(number + " ");
            }
        }
        
        scanner.close();
    }

    // Method to check if a number is an Armstrong number
    public static boolean isArmstrong(int num) {
        int originalNum = num;
        int sum = 0;
        int digits = String.valueOf(num).length(); // Count digits

        // Calculate sum of digits raised to the power of number of digits
        while (num > 0) {
            int digit = num % 10; // Get last digit
            sum += Math.pow(digit, digits); // Raise digit to power and add to sum
            num /= 10; // Remove last digit
        }

        // Check if sum is equal to original number
        return sum == originalNum;
    }
}

Explanation of the Code

  1. Importing Scanner: We import java.util.Scanner for taking user input.
  2. Main Method
    We prompt the user for a lower and upper bound.
    We loop through each integer in this range and call isArmstrong() to check if it is an Armstrong number.
  3. isArmstrong Method:
    This method takes an integer as input and checks whether it is an Armstrong number.
    It counts the digits using String.valueOf(num).length().
    It calculates the sum of each digit raised to the power of the total number of digits.
    Finally, it compares this sum with the original number.

Example Output

When you run this program, you might see output like this.

Armstrong Number in Java Between 100 to 500

Conclusion

In this article, we created a Java program to check for Armstrong numbers between two integers provided by the user. The program effectively demonstrates how to use loops, methods, and mathematical operations in Java. You can enhance this program further by adding error handling or allowing users to input ranges with validation.


Similar Articles