Last Updated: June 7, 2026
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.
d before it can raise any digit to a power.2^31 - 1 produces digit powers that add up beyond the range of a 32-bit integer, so accumulate the sum in a long.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.
n so you can compare against it later.d. Treat 0 as having one digit.n % 10.d and add the result to a long running sum.n / 10 and repeat until no digits remain.true if the running sum equals the original number, and false otherwise.Input:
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.
d is the number of digits. Since d is about log10(n), this is O(log n). Each digit is raised to a power with a loop of length d, so the exact work is O(d^2), and d is at most 10 here, which stays effectively constant.n.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.
n to a string and read its length as the exponent d.long running sum to 0.d and add the result to the running sum.true if the sum equals n, and false otherwise.Input:
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.
d is the number of digits, which is about O(log n). Raising each digit to the power d with an inner loop makes the exact work O(d^2), and d is at most 10 for a 32-bit input.