Armstrong Number

Java coding interview problem for Basic Number Programs: Armstrong Number.

Checking whether a number is an Armstrong Number is one of the most popular Java coding interview questions. It helps interviewers evaluate your understanding of loops, mathematical operations, digit extraction, exponentiation, and logical problem-solving.


What is an Armstrong Number?

An Armstrong Number (also known as a Narcissistic Number) is a number that is equal to the sum of each digit raised to the power of the total number of digits.

Mathematically,

Armstrong Number

=

(d₁ⁿ + d₂ⁿ + d₃ⁿ + ... + dₙⁿ)

Where

  • d = each digit
  • n = total number of digits

Example 1

Number

153

Digits

1

5

3

Total digits

3

Calculation

1³ + 5³ + 3³

=

1 + 125 + 27

=

153

Since

153 = 153

It is an Armstrong Number.


Example 2

Number

9474

Digits

9

4

7

4

Total digits

4

Calculation

9⁴

+

4⁴

+

7⁴

+

4⁴

=

6561

+

256

+

2401

+

256

=

9474

Therefore,

9474 is an Armstrong Number

Examples

Number Armstrong?
153 ✅ Yes
370 ✅ Yes
371 ✅ Yes
407 ✅ Yes
9474 ✅ Yes
123 ❌ No
100 ❌ No

Real Interview Question

Write a Java program to determine whether a given number is an Armstrong Number.


Example 1

Input

153

Output

153 is an Armstrong Number

Example 2

Input

123

Output

123 is NOT an Armstrong Number

Understanding the Logic

To determine whether a number is an Armstrong Number,

  1. Count the total number of digits.
  2. Extract every digit.
  3. Raise each digit to the power of the digit count.
  4. Add all the powered values.
  5. Compare the final sum with the original number.

If both are equal,

Armstrong Number

Otherwise,

Not an Armstrong Number

Brute Force Approach

The simplest solution is

  1. Count digits.
  2. Reverse through every digit.
  3. Compute
digit^count
  1. Add all values.
  2. Compare with the original number.

Algorithm

Step 1

Read the input number.

Step 2

Store the original number.

Step 3

Count the total digits.

Step 4

Reset the number.

Step 5

Extract each digit.

digit = number % 10;

Step 6

Calculate

Math.pow(digit, digitCount)

Step 7

Add the result to the sum.

Step 8

Remove the last digit.

number = number / 10;

Step 9

Repeat until the number becomes zero.

Step 10

Compare

sum == original

Dry Run

Input

153

Digit Count

3
Iteration Digit Calculation Sum
1 3 3³ = 27 27
2 5 5³ = 125 152
3 1 1³ = 1 153

Original

153

Calculated Sum

153

Output

Armstrong Number

Dry Run (Non-Armstrong)

Input

123

Digit Count

3
Iteration Digit Calculation Sum
1 3 27 27
2 2 8 35
3 1 1 36

Original

123

Calculated Sum

36

Output

Not an Armstrong Number

Approach 1 — Using Math.pow()

Complete Java Program

public class ArmstrongNumber {

    public static void main(String[] args) {

        int number = 153;

        int original = number;

        int digitCount = 0;

        int sum = 0;

        while (number != 0) {

            digitCount++;

            number = number / 10;

        }

        number = original;

        while (number != 0) {

            int digit = number % 10;

            sum += Math.pow(digit, digitCount);

            number = 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

Step-by-Step Code Explanation

Step 1

Read the input number.

int number = 153;

Step 2

Store the original number.

int original = number;

This is required because

number

changes while processing the digits.


Step 3

Count the digits.

while (number != 0)

Example

153

↓

15

↓

1

↓

0

Digit count becomes

3

Step 4

Restore the original number.

number = original;

Now we can process every digit again.


Step 5

Extract the last digit.

digit = number % 10;

For

153

Digits are extracted as

3

5

1

Step 6

Raise each digit to the power of the total digit count.

Math.pow(digit, digitCount)

Example

3³ = 27

5³ = 125

1³ = 1

Step 7

Add every calculated value.

sum += Math.pow(digit, digitCount);

Running total

27

↓

152

↓

153

Step 8

Remove the last digit.

number = number / 10;

Continue until

number = 0

Step 9

Compare

sum == original

If both are equal,

Armstrong Number

Otherwise,

Not an Armstrong Number

Why Does This Work?

Every digit contributes to the final result by being raised to the power of the total number of digits.

If the sum of these powered values equals the original number, then the mathematical definition of an Armstrong Number is satisfied.


Drawbacks of This Approach

The solution uses Math.pow(), which returns a double. Although it works correctly for common interview examples, it introduces floating-point calculations and can be slightly slower than pure integer arithmetic.

we'll improve the solution by creating a reusable method, explore an integer-only implementation, analyze time and space complexity, discuss common mistakes, and cover frequently asked interview follow-up questions.

Approach 2 — Using a Reusable Method

Instead of writing the complete logic inside the main() method, create a reusable method.

This approach improves

  • Code reusability
  • Readability
  • Unit testing
  • Maintainability

Java Solution

public class ArmstrongNumberMethod {

    static boolean isArmstrong(int number) {

        int original = number;

        int digitCount = String.valueOf(number).length();

        int sum = 0;

        while (number != 0) {

            int digit = number % 10;

            sum += Math.pow(digit, digitCount);

            number = number / 10;

        }

        return sum == original;

    }

    public static void main(String[] args) {

        int number = 9474;

        if (isArmstrong(number)) {

            System.out.println(number + " is an Armstrong Number");

        } else {

            System.out.println(number + " is NOT an Armstrong Number");

        }

    }

}

Output

9474 is an Armstrong Number

Approach 3 — Without Using Math.pow()

Although Math.pow() is simple, it returns a double.

Many interviewers prefer solving the problem using only integer arithmetic.


Java Solution

public class ArmstrongWithoutMathPow {

    static int power(int base, int exponent) {

        int result = 1;

        for (int i = 1; i <= exponent; i++) {

            result *= base;

        }

        return result;

    }

    public static void main(String[] args) {

        int number = 153;

        int original = number;

        int digitCount = String.valueOf(number).length();

        int sum = 0;

        while (number != 0) {

            int digit = number % 10;

            sum += power(digit, digitCount);

            number = 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

Time Complexity

Using Math.pow()

Operation Complexity
Time O(d²)
Space O(1)

Where

d = Number of Digits

Integer Power Method

Operation Complexity
Time O(d²)
Space O(1)

Comparison

Approach Time Space Recommended
Using Math.pow() O(d²) O(1) ✅ Best for Interviews
Reusable Method O(d²) O(1) Reusable
Integer Power Method O(d²) O(1) Avoids Floating Point

Common Mistakes

Mistake 1

Not restoring the original number.

Wrong

number = number / 10;

After counting digits,

the value becomes

0

Always restore it.

number = original;

Mistake 2

Using a fixed power.

Wrong

digit * digit * digit

This only works for

3-digit

numbers.

Armstrong Numbers can contain

4

5

6

...

digits.

Always calculate

digitCount

dynamically.


Mistake 3

Forgetting to reset

sum

before processing.

Always initialize

sum = 0;

Mistake 4

Using

Math.pow()

without converting to an integer.

Correct

sum += (int) Math.pow(digit, digitCount);

or rely on compound assignment as shown.


Mistake 5

Ignoring single-digit numbers.

Every single-digit number

0

1

2

...

9

is an Armstrong Number.


Interview Follow-up Questions

Q1. Print all Armstrong Numbers between 1 and N.

Q2. Count Armstrong Numbers in a given range.

Q3. Find the next Armstrong Number.

Q4. Check whether a very large number is an Armstrong Number.

Q5. Write the solution without using Math.pow().

Q6. Write a reusable isArmstrong() method.

Q7. Find all 4-digit Armstrong Numbers.

Q8. Compare Armstrong and Perfect Numbers.

Q9. Explain the time complexity of your solution.

Q10. Optimize the solution for repeated checks.


Related Coding Problems

  • Palindrome Number
  • Reverse Integer
  • Prime Number
  • Perfect Number
  • Strong Number
  • Neon Number
  • Fibonacci Series
  • Power of a Number

Key Takeaways

  • An Armstrong Number equals the sum of each digit raised to the power of the total number of digits.
  • First count the digits, then process each digit.
  • The solution requires digit extraction using % and /.
  • Math.pow() makes the implementation simple and readable.
  • An integer power method avoids floating-point calculations.
  • The algorithm uses constant extra space and is suitable for Java coding interviews.

Interview Tip

If an interviewer asks:

"Write a Java program to check whether a number is an Armstrong Number."

Start by explaining the mathematical definition. Implement the solution using digit extraction with % and /, count the number of digits, and calculate the sum of each digit raised to the digit count. Mention that Math.pow() is convenient but returns a double, and discuss how an integer-only power function can avoid floating-point operations. This demonstrates both practical coding ability and awareness of implementation trade-offs.