Check Whether a Number Is an Armstrong Number
Determine whether a number equals the sum of its digits each raised to the number of digits.
Count how many digits the number has, then add up each digit raised to that count. If the total equals the original number, it is an Armstrong number. For 153 with 3 digits, 1^3 + 5^3 + 3^3 = 153, so 153 is an Armstrong number.
Problem Statement
An Armstrong number (also called a narcissistic number) is a number that equals the sum of each of its digits raised to the power of the total number of digits. For a 3-digit number, you cube each digit; for a 4-digit number, you raise each digit to the fourth power.
Given a non-negative integer, return true if it is an Armstrong number and
false otherwise. For example, 153 = 1^3 + 5^3 + 3^3, so 153 is an Armstrong
number.
Input: A single non-negative integer n.
Output: true if n is an Armstrong number, otherwise false.
Examples
Input: 153
Output: true153 has 3 digits and 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
Input: 123
Output: false123 has 3 digits and 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36, which is not 123.
Constraints
0 <= n <= 2,147,483,647 (fits in a 32-bit int)Use the digit count as the exponent, not a fixed power of 3
Think Before You Code
Reveal the questions to ask yourself first
- How many digits does the number have, and why does that count become the exponent?
- How do you raise a single digit to a power in Java?
- Why must you keep the original number untouched while you extract its digits?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Compute the digit-power sum and compare it with the original number.
- Count the digits
dofn(a 0 has 1 digit). - Set
sum = 0and copyninto a temporary variable. - While the temporary value is not 0:
digit = temp % 10.sum += digit^d(useMath.pow, cast back to int).temp = temp / 10.
- Return whether
sumequals the originaln.
Dry Run
Walk through the example step by step
Checking n = 153 (3 digits):
step | temp | digit | digit^3 | sum
-----+------+-------+---------+-----
1 | 153 | 3 | 27 | 27
2 | 15 | 5 | 125 | 152
3 | 1 | 1 | 1 | 153
end | 0 | — | — | 153
sum (153) == original (153) -> true.
Solution
Reveal the full Java solution
public class ArmstrongNumber {
public static boolean isArmstrong(int n) {
if (n < 0) {
return false;
}
int digits = countDigits(n);
int sum = 0;
int temp = n;
while (temp != 0) {
int digit = temp % 10;
sum += (int) Math.pow(digit, digits);
temp /= 10;
}
return sum == n;
}
private static int countDigits(int n) {
if (n == 0) {
return 1;
}
int count = 0;
while (n != 0) {
count++;
n /= 10;
}
return count;
}
public static void main(String[] args) {
System.out.println(isArmstrong(153)); // true
System.out.println(isArmstrong(123)); // false
}
}
The exponent is the number of digits, so you must count the digits before summing.
Math.pow returns a double; casting the result back to int is safe here because
each digit raised to a small power stays well within int range for typical inputs.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Always cubing the digits instead of raising them to the actual digit count, which breaks for 4-digit numbers.
- Overwriting the original number while extracting digits, leaving nothing to compare against.
Edge Cases to Test
- Single-digit numbers 0-9 are all Armstrong numbers (d^1 == d).
- 0 should return true because it has one digit and 0^1 = 0.
Interview Follow-Ups
- How would you print every Armstrong number between 1 and 1000?
- How would you avoid Math.pow and compute the power with an integer loop instead?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →