Java ProgramsNumbersCheck Armstrong Number

Check Armstrong Number in Java

beginner·  Numbers  ·  Number Theory

Problem

An Armstrong number (also called a narcissistic number) is a number equal to the sum of its own digits, each raised to the power of the total digit count.

Given a number, determine whether it is an Armstrong number.

Input
153
Output
153 is an Armstrong number: true

Java Program

Java
public class ArmstrongCheck { public static void main(String[] args) { int num = 153; int digitCount = String.valueOf(num).length(); int original = num; int sum = 0; while (num > 0) { int digit = num % 10; // peel off the last digit sum += (int) Math.pow(digit, digitCount); num /= 10; // drop the digit just processed } System.out.println(original + " is an Armstrong number: " + (sum == original)); } }

Output

153 is an Armstrong number: true

Core Logic

Extracting each digit with % and /, raising it to the power of the digit count, and summing the results checks the definition directly.

How It Works
  1. 1digitCount is found by converting the number to a String and reading its length().
  2. 2The loop peels off one digit at a time with num % 10, then removes it from num with num /= 10.
  3. 3Each digit is raised to the power of digitCount with Math.pow() and added into sum.
  4. 4Once every digit has been processed, sum is compared against the original number.
For 153 (3 digits), the digits 1, 5, 3 raised to the third power give 1, 125, and 27 — summing to 153, which matches the original number.
💡

Key Point: Math.pow() returns a double, so the result is cast back to int before adding it to the running sum — with small digit counts this never loses precision.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The loop runs once per digit, so the work scales with the number of digits, not the number's magnitude, and only a few running variables are kept.

Key Concepts

digit extractionMath.pow()modulo operator

Approach 2: Java 8

Java
public class ArmstrongCheckStream { public static void main(String[] args) { int num = 153; int digitCount = String.valueOf(num).length(); // Converts each digit character back to a number, raises it to the digit count, and sums int sum = String.valueOf(num).chars() .map(c -> (int) Math.pow(c - '0', digitCount)) .sum(); System.out.println(num + " is an Armstrong number: " + (sum == num)); } }

Output

153 is an Armstrong number: true

Core Logic

The same digit-by-digit power sum can be expressed as a stream over the number's character digits.

How It Works
  1. 1String.valueOf(num).chars() returns an IntStream of the number's digit characters.
  2. 2.map(c -> (int) Math.pow(c - '0', digitCount)) converts each character back to a digit with c - '0', then raises it to the digit count.
  3. 3.sum() reduces the stream of powered digits down to a single total.
For 153, the stream maps '1', '5', '3' to 1, 125, and 27, and .sum() adds them up to 153.
💡

Key Point: c - '0' is the standard trick for converting a digit character to its numeric value, relying on digit characters being contiguous in Unicode.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The stream still visits each digit character once, and sum() reduces directly to a single int without collecting anything.

Key Concepts

Streamchars()sum()

Related Programs