Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 50

Armstrong Number Program in Java

In this lesson: Check Armstrong number in Java with digits and power — code, step-by-step dry run and output. Copy & run. In Hindi & English.

What is an Armstrong number

An Armstrong number (for 3 digits) equals the sum of the cubes of its own digits. The classic example is 153: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. Checking one is a great exercise in extracting digits and using a loop.

💡 153 is Armstrong because the cubes of its digits add back up to 153.

Extracting digits

int num = 153;
while (num > 0) {
    int digit = num % 10;   // last digit
    System.out.println(digit);
    num = num / 10;         // remove last digit
}
3
5
1

The trick % 10 gets the last digit and / 10 removes it. Repeating peels digits off from right to left until the number becomes 0.

The Java program

public class Armstrong {
    public static void main(String[] args) {
        int num = 153;
        int original = num;    // keep a copy
        int sum = 0;

        while (num > 0) {
            int d = num % 10;
            sum += d * d * d;  // cube each digit
            num = num / 10;
        }
        if (sum == original)
            System.out.println(original + " is Armstrong");
        else
            System.out.println(original + " is not Armstrong");
    }
}
153 is Armstrong

We save original, then loop: take each digit, add its cube to sum, and remove the digit. At the end we compare sum with the original number.

Dry run

Dry Run — is 153 Armstrong? (cube each digit and add)
Steptempd = temp%10sum += d*d*dtemp /= 10
115330 + 27 = 2715
215527 + 125 = 1521
311152 + 1 = 1530
40temp==0 stops. sum(153) == num(153) → Armstrong!
💡 Understand it this way

Each turn peels off the last digit with %10, cubes it into sum, then drops it with /10. When the sum of cubes equals the original number, it is Armstrong.

For any number of digits

A true Armstrong number raises each digit to the power of the number of digits, not always 3. For a 4-digit number, you cube... no, you use the 4th power. So count the digits first, then raise each digit to that power. The 3-digit version above uses cubes because it has 3 digits.

Common mistakes

  • Not saving the original. The loop reduces the number to 0 — keep a copy to compare.
  • Wrong power. For n digits, raise each digit to the n-th power, not always 3.
  • Swapping % and /. % 10 gets the digit; / 10 removes it.
  • Wrong loop condition. Loop while num > 0 to process all digits.

Summary

  • An Armstrong number equals the sum of its digits raised to the digit-count power.
  • For 3 digits, that means the sum of cubes (153 = 1³+5³+3³).
  • Get digits with % 10 and remove them with / 10.
  • Save the original number to compare at the end.
  • Compare the sum to the original to decide.

Next, practise digit work in the palindrome number program, or loops in Java loops.

Frequently Asked Questions

What is an Armstrong number?
An Armstrong number (for 3 digits) is a number equal to the sum of the cubes of its digits. For example 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. Such numbers are also called narcissistic numbers.
How do I extract the digits of a number?
Use digit = num % 10 to get the last digit, then num = num / 10 to remove it. Repeat in a loop until num becomes 0. This peels off digits from right to left.
How does the Armstrong check work?
Take each digit, cube it (for a 3-digit number), and add to a running sum. After processing all digits, compare the sum to the original number. If they are equal, it is an Armstrong number.
What are some examples of Armstrong numbers?
Common 3-digit Armstrong numbers are 153, 370, 371 and 407. Single digits 0-9 are trivially Armstrong. For more digits, the power used equals the number of digits.
Why do we save the number before the loop?
The loop changes the number as it removes digits, so it ends at 0. We keep a copy of the original in another variable to compare against the digit-power sum at the end.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicFactorial (Recursion) Your progress: 50 / 61 (82%) · Lesson 51 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free