Learn what an Armstrong number is and how to check for one. Runnable code examples in Java, Python, C, and C++ — execute them instantly in your browser with JDoodle.
Code Examples

Armstrong Number Program - Java, Python, C, C++ Examples

JDoodle Team

An Armstrong number (also called a narcissistic number) is a number that equals the sum of its own digits, each raised to the power of the total number of digits. For example, 153 is an Armstrong number because:

1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Armstrong numbers are a classic programming exercise used in computer science courses worldwide to practice loops, conditionals, and modular arithmetic. In this guide, you’ll find working code examples in Java, Python, C, and C++ — all runnable directly in your browser using JDoodle’s online compilers.

How Armstrong Numbers Work

For a number with n digits, it is an Armstrong number if:

d₁ⁿ + d₂ⁿ + d₃ⁿ + ... + dₙⁿ = the number itself

3-digit examples:

  • 153 → 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✓
  • 370 → 3³ + 7³ + 0³ = 27 + 343 + 0 = 370 ✓
  • 371 → 3³ + 7³ + 1³ = 27 + 343 + 1 = 371 ✓
  • 407 → 4³ + 0³ + 7³ = 64 + 0 + 343 = 407 ✓

4-digit example:

  • 1634 → 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1 + 1296 + 81 + 256 = 1634 ✓

Not an Armstrong number:

  • 123 → 1³ + 2³ + 3³ = 1 + 8 + 27 = 36 ≠ 123 ✗

Step-by-Step Algorithm

  1. Take the input number
  2. Count the total number of digits (call it n)
  3. Extract each digit one by one
  4. Raise each digit to the power of n
  5. Sum all the powered digits
  6. If the sum equals the original number → it’s an Armstrong number

Armstrong Number in Java

This Java program checks if a given number is an Armstrong number and handles any number of digits:

// Armstrong Number Checker in Java
// Run this code on JDoodle: https://www.jdoodle.com/online-java-compiler

import java.util.Scanner;

public class ArmstrongNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        int originalNumber = number;
        int sum = 0;
        int digits = String.valueOf(number).length();

        int temp = number;
        while (temp > 0) {
            int digit = temp % 10;
            sum += Math.pow(digit, digits);
            temp /= 10;
        }

        if (sum == originalNumber) {
            System.out.println(number + " is an Armstrong number");
        } else {
            System.out.println(number + " is not an Armstrong number");
        }

        // Bonus: Find all Armstrong numbers between 1 and 10000
        System.out.println("\nAll Armstrong numbers from 1 to 10000:");
        for (int i = 1; i <= 10000; i++) {
            int s = 0;
            int d = String.valueOf(i).length();
            int t = i;
            while (t > 0) {
                int digit = t % 10;
                s += Math.pow(digit, d);
                t /= 10;
            }
            if (s == i) {
                System.out.print(i + " ");
            }
        }
    }
}

Input: 153 Output:

153 is an Armstrong number

All Armstrong numbers from 1 to 10000:
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474

▶ Run this code on JDoodle

Armstrong Number in Python

Python makes this especially clean thanks to built-in string conversion and list comprehensions:

# Armstrong Number Checker in Python
# Run this code on JDoodle: https://www.jdoodle.com/python3-programming-online

def is_armstrong(num):
    digits = str(num)
    power = len(digits)
    total = sum(int(d) ** power for d in digits)
    return total == num

# Check a single number
number = int(input("Enter a number: "))
if is_armstrong(number):
    print(f"{number} is an Armstrong number")
else:
    print(f"{number} is not an Armstrong number")

# Show the breakdown
digits = str(number)
power = len(digits)
parts = [f"{d}^{power}" for d in digits]
values = [int(d) ** power for d in digits]
print(f"\nBreakdown: {' + '.join(parts)}")
print(f"         = {' + '.join(str(v) for v in values)}")
print(f"         = {sum(values)}")

# Find all Armstrong numbers up to 10000
print("\nAll Armstrong numbers from 1 to 10000:")
armstrong_list = [i for i in range(1, 10001) if is_armstrong(i)]
print(armstrong_list)

Input: 9474 Output:

9474 is an Armstrong number

Breakdown: 9^4 + 4^4 + 7^4 + 4^4
         = 6561 + 256 + 2401 + 256
         = 9474

All Armstrong numbers from 1 to 10000:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]

▶ Run this code on JDoodle

Armstrong Number in C

The C implementation uses a while loop to extract digits and the pow() function from math.h:

// Armstrong Number Checker in C
// Run this code on JDoodle: https://www.jdoodle.com/c-online-compiler

#include <stdio.h>
#include <math.h>

int isArmstrong(int num) {
    int original = num;
    int sum = 0;
    int digits = 0;
    int temp = num;

    // Count digits
    while (temp > 0) {
        digits++;
        temp /= 10;
    }

    // Calculate sum of powered digits
    temp = num;
    while (temp > 0) {
        int digit = temp % 10;
        sum += (int)pow(digit, digits);
        temp /= 10;
    }

    return sum == original;
}

int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    if (isArmstrong(number)) {
        printf("%d is an Armstrong number\n", number);
    } else {
        printf("%d is not an Armstrong number\n", number);
    }

    // Find all Armstrong numbers from 1 to 10000
    printf("\nAll Armstrong numbers from 1 to 10000:\n");
    for (int i = 1; i <= 10000; i++) {
        if (isArmstrong(i)) {
            printf("%d ", i);
        }
    }
    printf("\n");

    return 0;
}

▶ Run this code on JDoodle

Armstrong Number in C++

// Armstrong Number Checker in C++
// Run this code on JDoodle: https://www.jdoodle.com/online-compiler-c++

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

bool isArmstrong(int num) {
    int original = num;
    int digits = to_string(num).length();
    int sum = 0;

    while (num > 0) {
        int digit = num % 10;
        sum += pow(digit, digits);
        num /= 10;
    }

    return sum == original;
}

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    if (isArmstrong(number)) {
        cout << number << " is an Armstrong number" << endl;
    } else {
        cout << number << " is not an Armstrong number" << endl;
    }

    cout << "\nArmstrong numbers from 1 to 10000: ";
    for (int i = 1; i <= 10000; i++) {
        if (isArmstrong(i)) cout << i << " ";
    }
    cout << endl;

    return 0;
}

▶ Run this code on JDoodle

Complete List of Armstrong Numbers

Here are all known Armstrong numbers up to 1 million:

DigitsArmstrong Numbers
1 digit1, 2, 3, 4, 5, 6, 7, 8, 9
3 digits153, 370, 371, 407
4 digits1634, 8208, 9474
5 digits54748, 92727, 93084
6 digits548834

Note: There are no 2-digit Armstrong numbers. The sum of squares of any two-digit number’s digits never equals the number itself.

Time and Space Complexity

AspectComplexity
Time complexityO(d) where d = number of digits
Space complexityO(1) — only fixed variables used
Finding all Armstrong numbers in range [1, N]O(N × d)

The algorithm is efficient because it only needs to process each digit once, making it O(d) where d is the number of digits in the input.

Common Variations and Practice Problems

Once you’ve understood the basic Armstrong number check, try these exercises to deepen your understanding:

  1. Find all Armstrong numbers in a range

Modify the program to accept two numbers and find all Armstrong numbers between them.

  1. Armstrong number using recursion

Rewrite the check function without using loops.

  1. N-digit Armstrong numbers

Find all Armstrong numbers with exactly N digits.

  1. Sum of Armstrong numbers

Calculate the sum of all Armstrong numbers below a given limit.

  1. Nearest Armstrong number

Given any number, find the closest Armstrong number.

You can try all of these exercises directly in JDoodle’s online compilers — no setup needed.

FAQs

1. What is an Armstrong number?

An Armstrong number (narcissistic number) is a number equal to the sum of its digits each raised to the power of the total number of digits. For example, 153 = 1³ + 5³ + 3³.

2. What are the Armstrong numbers between 1 and 1000?

The Armstrong numbers between 1 and 1000 are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, and 407.

3. Is 0 an Armstrong number?

By the mathematical definition, 0 is considered an Armstrong number since 0 raised to any power equals 0. However, many programming implementations start checking from 1.

4. Why are there no 2-digit Armstrong numbers?

For a 2-digit number “ab”, you would need a² + b² = 10a + b. The maximum value of a² + b² when both digits are 9 is only 162, and no 2-digit combination satisfies this equation.

5. What is the largest known Armstrong number?

The largest known Armstrong number has 39 digits: 115,132,219,018,763,992,565,095,597,973,971,522,401.

6. How is an Armstrong number different from a perfect number?

A perfect number equals the sum of its proper divisors (e.g., 6 = 1 + 2 + 3). An Armstrong number equals the sum of its digits raised to a power. They are completely different mathematical concepts.

Dive Deeper into More Topics

Get ready to go beyond the surface and explore a treasure trove of diverse topics!