LeetCode 1134: Armstrong Number Solution

Master LeetCode problem 1134 (Armstrong Number), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1134. Armstrong Number

Easy

Problem Explanation

Explanation:

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

To determine if a given number is an Armstrong number, we need to calculate the sum of the digits of the number raised to the power of the number of digits and compare it with the original number. :

Solution Code

class Solution {
    public boolean isArmstrong(int n) {
        int originalNum = n;
        int numDigits = String.valueOf(n).length();
        int armstrongSum = 0;
        
        while (n > 0) {
            int digit = n % 10;
            armstrongSum += Math.pow(digit, numDigits);
            n /= 10;
        }
        
        return armstrongSum == originalNum;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1134 (Armstrong Number)?

This page provides optimized solutions for LeetCode problem 1134 (Armstrong Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1134 (Armstrong Number)?

The time complexity for LeetCode 1134 (Armstrong Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1134 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1134 in Java, C++, or Python.

Back to LeetCode Solutions