Armstrong Number in Java: Methods & Examples Explained

Introduction

An Armstrong number (also known as a narcissistic number, pluperfect number, or pluperfect digital invariant, PPDI) is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. In this blog, we will explore Armstrong numbers, their significance, and different ways to check for Armstrong numbers using Java.

What is an Armstrong Number?

A number is an Armstrong number if:

sum of (each digit raised to the power of number of digits)=original number\text{sum of (each digit raised to the power of number of digits)} = \text{original number}

Example:

  • 153 is an Armstrong number because: 13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
  • 9474 is an Armstrong number because: 94+44+74+44=6561+256+2401+256=94749^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474

Methods to Check Armstrong Number in Java

We will explore different methods to check if a number is an Armstrong number using Java.

1. Using a Basic Loop

This method involves extracting each digit, raising it to the power of the number of digits, summing them up, and comparing with the original number.

import java.util.Scanner;

public class ArmstrongNumber {
    public static boolean isArmstrong(int number) {
        int originalNumber = number;
        int sum = 0;
        int numDigits = String.valueOf(number).length();
        
        while (number > 0) {
            int digit = number % 10;
            sum += Math.pow(digit, numDigits);
            number /= 10;
        }
        return sum == originalNumber;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        scanner.close();
        
        if (isArmstrong(num)) {
            System.out.println(num + " is an Armstrong number.");
        } else {
            System.out.println(num + " is not an Armstrong number.");
        }
    }
}

2. Using Recursion

A recursive approach can be implemented to calculate the Armstrong number.

public class ArmstrongRecursion {
    public static int armstrongSum(int number, int numDigits) {
        if (number == 0) return 0;
        int digit = number % 10;
        return (int)Math.pow(digit, numDigits) + armstrongSum(number / 10, numDigits);
    }

    public static boolean isArmstrong(int number) {
        return number == armstrongSum(number, String.valueOf(number).length());
    }
    
    public static void main(String[] args) {
        int num = 9474;
        System.out.println(num + " is " + (isArmstrong(num) ? "an Armstrong number." : "not an Armstrong number."));
    }
}

3. Checking Armstrong Numbers in a Given Range

To check and print all Armstrong numbers in a given range:

public class ArmstrongRange {
    public static boolean isArmstrong(int number) {
        int originalNumber = number;
        int sum = 0;
        int numDigits = String.valueOf(number).length();
        
        while (number > 0) {
            int digit = number % 10;
            sum += Math.pow(digit, numDigits);
            number /= 10;
        }
        return sum == originalNumber;
    }
    
    public static void main(String[] args) {
        int lower = 1, upper = 10000;
        System.out.println("Armstrong numbers between " + lower + " and " + upper + ":");
        
        for (int i = lower; i <= upper; i++) {
            if (isArmstrong(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Applications of Armstrong Numbers

  1. Mathematical Significance: Used in number theory to study patterns in digits.
  2. Computer Science and Programming: Often used as an example in programming exercises and coding interviews.
  3. Cryptography: Certain numeric properties, like Armstrong numbers, can be applied in cryptographic algorithms.

Conclusion

Armstrong numbers are an interesting mathematical concept with applications in programming and number theory. We explored different methods to check Armstrong numbers using Java, including basic loops, recursion, and range-based checking.

🔎 Take the Data Science Entrance Test – Boost your career with Cuvette’s placement guarantee! Take the test, and our team will contact you: Start the test now