Learner Level

Implement the isArmstrongNumber method that checks whether a number is equal to the sum of its digits raised to the digit count.

The input contains an integer n. Your task is to check whether n is an Armstrong number.

An Armstrong number is equal to the sum of its digits raised to the power of the number of digits. For example, 153 has three digits, and 1^3 + 5^3 + 3^3 = 153, so it is an Armstrong number.

Example 1

Input
n (int) = 153
Returns (boolean) true

Example 2

Input
n (int) = 9474
Returns (boolean) true

Example 3

Input
n (int) = 123
Returns (boolean) false

First count how many digits are present in the number.

Then process each digit, raise it to the power of the digit count, and add it to a running sum. At the end, compare the sum with the original number. If both are equal, return true; otherwise return false.

Pseudocode:

function isArmstrongNumber(n):
    original = n
    digits = count digits in n
    sum = 0
    while n > 0:
        digit = n % 10
        sum = sum + power(digit, digits)
        n = n / 10
    return sum == original
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.