Armstrong Number Check in Java: Explanation & Practice

Check if a number is an Armstrong number

Problem summary

An Armstrong number equals the sum of its digits each raised to the power of digit count. Check if the given number is Armstrong.

Starter code

public class Main {
    public static void main(String[] args) {
        int number = 153; // Test case 1
        
        // Check if Armstrong: 153 = 1^3 + 5^3 + 3^3
        // Print: Is Armstrong: <true/false>
    }
}

Expected output and test cases

  • 153 = 1³+5³+3³
    Is Armstrong: true
  • 9474 = 9⁴+4⁴+7⁴+4⁴
    Is Armstrong: true
  • 123 is not Armstrong
    Is Armstrong: false

Hints

  1. First count the number of digits
  2. Extract each digit and raise to power of digit count
  3. Sum all powered digits and compare with original

Related Core Java Basics exercises

Practice all Core Java Basics exercises · Run this idea in the Java compiler