AlgoMaster Logo

Armstrong Number Check

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

An Armstrong number ties two things together: the number of digits and the value of each digit. You cannot raise a digit to the right power until you know how many digits the number has. So the work splits in two. First, count the digits to get the exponent d. Second, raise each digit to the power d, add the results, and check whether the total matches the original number.

Both parts use the same arithmetic. n % 10 peels off the last digit, and n / 10 removes it by shifting everything one place right. Repeating those two operations visits every digit without converting the number to text.

Key Constraints:

  • You need the digit count first → The exponent is the number of digits, so the algorithm has to determine d before it can raise any digit to a power.
  • The running sum can grow large → A ten-digit input near 2^31 - 1 produces digit powers that add up beyond the range of a 32-bit integer, so accumulate the sum in a long.
  • Zero and single digits are Armstrong numbers → A one-digit value raised to the first power is itself, so these cases fall out of the general logic without a special branch.

Approach 1: Count Digits, Then Sum of Powers

Intuition

Make one pass over a copy of the number to count its digits, giving the exponent d. Make a second pass to pull off each digit, raise it to the power d, and add it to a running total. If the total equals the original number, it is an Armstrong number.

Keep the running total in a long. For a ten-digit input, each digit can be 9, and 9^10 is 3486784401, which already exceeds the maximum 32-bit signed integer of 2147483647. Summing several such terms in a 32-bit int would overflow, so a wider type protects the comparison.

Algorithm

  1. Save the original value of n so you can compare against it later.
  2. Count the digits by repeatedly dividing a working copy by 10 until it reaches 0. This gives the exponent d. Treat 0 as having one digit.
  3. Reset to the original number and extract digits one at a time using n % 10.
  4. Raise each digit to the power d and add the result to a long running sum.
  5. Remove the processed digit with n / 10 and repeat until no digits remain.
  6. Return true if the running sum equals the original number, and false otherwise.

Example Walkthrough

Input:

153
n

The number 153 has three digits, so the exponent d is 3. Now extract digits from the right. The last digit is 3, and 3^3 = 27. Drop it to get 15. The next digit is 5, and 5^3 = 125. Drop it to get 1. The final digit is 1, and 1^3 = 1. The running sum is 27 + 125 + 1 = 153. Since this equals the original number 153, the answer is true.

true
output

Code

Approach 2: Digits via String

Intuition

Converting n to a string gives the digit count directly from its length and the digits in order from its characters. This removes the separate counting pass.

The core arithmetic stays the same. Convert each character back to a number, raise it to the power of the string length, and add it to a running sum. Use a long for that sum, since large inputs can push the total past the 32-bit range.

Algorithm

  1. Convert n to a string and read its length as the exponent d.
  2. Initialize a long running sum to 0.
  3. For each character in the string, convert it to its numeric digit value.
  4. Raise the digit to the power d and add the result to the running sum.
  5. After processing every character, return true if the sum equals n, and false otherwise.

Example Walkthrough

Input:

153
n

Converting 153 to a string gives "153", whose length is 3, so the exponent d is 3. Now walk the characters left to right. The character '1' becomes 1, and 1^3 = 1. The character '5' becomes 5, and 5^3 = 125. The character '3' becomes 3, and 3^3 = 27. The running sum is 1 + 125 + 27 = 153, which matches the original number, so the answer is true.

true
output

Code