Skip to content
CodeKruPro

Check whether a number is an Armstrong number

Coding problemEasyAsked in: Oracle, VMware

A number with k digits is an Armstrong number when the sum of its digits, each raised to the power k, equals the number itself. (Raising a digit to the power k means multiplying it by itself k times.) For example, 153 has 3 digits and 1 + 125 + 27 = 153.

Given a non-negative integer n, return true if it is an Armstrong number, and false otherwise. Every one-digit number, including 0, is an Armstrong number.

Examples

Example 1
Input: n = 153
Output: true
Example 2
Input: n = 9474
Output: true
9 to the 4th + 4 to the 4th + 7 to the 4th + 4 to the 4th = 6561 + 256 + 2401 + 256 = 9474.
Example 3
Input: n = 154
Output: false

Constraints

  • n is between 0 and 2,147,483,647

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in