SwadhyAI Notes

All Java notes

How to Check Armstrong Number in Java

What is an Armstrong Number?

An Armstrong number equals the sum of its digits raised to the power of the number of digits. Example: 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Solution

java
public class Main {
    public static void main(String[] args) {
        int number = 153;
        int original = number;
        int sum = 0;
        int digits = String.valueOf(number).length();
        
        while (number > 0) {
            int digit = number % 10;
            sum += Math.pow(digit, digits);
            number /= 10;
        }
        
        if (sum == original) {
            System.out.println(original + " is an Armstrong number");
        } else {
            System.out.println(original + " is not an Armstrong number");
        }
    }
}
// Output: 153 is an Armstrong number

Armstrong Numbers Examples

  • 1-digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (all single digits)
  • 3-digit: 153 (1³+5³+3³), 370, 371, 407
  • 4-digit: 1634 (1⁴+6⁴+3⁴+4⁴), 8208, 9474

Using a Method

java
public static boolean isArmstrong(int num) {
    int original = num;
    int sum = 0;
    int digits = String.valueOf(num).length();
    
    while (num > 0) {
        int digit = num % 10;
        sum += Math.pow(digit, digits);
        num /= 10;
    }
    
    return sum == original;
}

// Usage
System.out.println(isArmstrong(153));  // true
System.out.println(isArmstrong(123));  // false

Did You Know?

Armstrong numbers are also called "narcissistic numbers" because they're "in love with themselves" - they equal the sum of their own digits raised to a power!

Pro Tip

There are only 88 Armstrong numbers in base 10! The largest is 115,132,219,018,763,992,565,095,597,973,971,522,401

Real-World Use Cases

  • Number theory: Mathematical research and patterns
  • Programming practice: Common interview question
  • Recreational math: Number puzzles and games
›Short versionOpen if you only have a minute.
  • An Armstrong number equals the sum of its digits each raised to the digit count. 153 = 1^3 + 5^3 + 3^3.
  • Count the digits first. Cubing every number misses 1634, which uses the fourth power.
  • Keep a copy of the original. The loop that peels digits destroys the value you must compare against.
  • Math.pow returns a double. Multiply in a long when the power must be exact.

Armstrong number

Check whether a number equals the sum of its digits each raised to the number of digits.

Working approach

  • Count digits on one copy.
  • Raise each digit of a second copy to that count and add.
  • Compare the sum with the untouched original.

Walk the sample

  1. 153 has 3 digits
  2. 1^3 + 5^3 + 3^3 = 1 + 125 + 27
  3. The sum is 153, so it matches

The trap

Math.pow(digit, digits) is a double. For a larger digit count the mantissa cannot hold every integer, and an exact Armstrong number fails the equality.

java
static long pow(int digit, int exponent) {
    long value = 1;
    for (int i = 0; i < exponent; i++) {
        value *= digit;
    }
    return value;
}

Why is a hard-coded cube wrong for 1634?

1634 has four digits. The definition uses the fourth power: 1^4 + 6^4 + 3^4 + 4^4 = 1634.

A method that always cubes will reject it. Count the digits and use that count as the exponent.

Time O(d), space O(1), where d is the digit count.

Easy to get wrong

  • Comparing against a number the loop already reduced to 0.
  • Hard-coding the exponent 3.