C++ Armstrong Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is an Armstrong Number?

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

Remember
n is Armstrong  ↔  n = sum of (each digit ^ k)
where k = number of digits in n

Example: 153 (k = 3)
  1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Also: 1–9 (one digit), 370, 371, 407
nDigit powersResult
1531³+5³+3³ = 153Armstrong
3703³+7³+0³ = 370Armstrong
1231³+2³+3³ = 36Not Armstrong
55¹ = 5Armstrong

How to Check an Armstrong Number in C++

Four clear steps — count digits first, then sum powers with that same exponent.

  1. Reject n <= 0 for this page’s positive-integer rule.
  2. Count digits into k (repeatedly divide by 10).
  3. Peel each digit with % 10 and add digit^k.
  4. Compare the sum with the original number.
MethodIdeaTimeBest for
Digit loop + iPowCount k, then sum d^kO(log n)Interviews, default
String digitsto_string, then power each charO(log n)Readable C++ variant
Range scanReuse the helperO(N log N)Listing Armstrong numbers

Pseudocode

Pseudocode
function IsArmstrong(n):
    if n <= 0:
        return false

    k = digitCount(n)
    sum = 0
    t = n
    while t > 0:
        d = t mod 10
        sum = sum + (d ^ k)
        t = t / 10

    return sum == n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Testsum(digit^k) == n, k = digits
Peel digitd = n % 10, then n /= 10
PowersUse integer iPow, not std::pow
1 digit1–9 are Armstrong
3 digits153, 370, 371, 407
1..2001–9 and 153

Live Preview

Enter a positive integer to see each digit power and the Armstrong verdict.

Positive whole numbers. Tap a chip or type a value.

Result
n = 153, k = 3
1³ + 5³ + 3³ = 1 + 125 + 27 = 153
Verdict: ARMSTRONG

Worked Walkthrough — n = 153

Count digits, raise each digit to that power, then compare.

Calculation
153 has 3 digits  →  k = 3

Digit 3: 3³ = 27
Digit 5: 5³ = 125
Digit 1: 1³ = 1

Sum = 27 + 125 + 1 = 153
153 == 153  → Armstrong

C++ Programs

Start with a single check using integer powers. Then list a range, and finish with a readable string-based digit walk.

Example 1 — Check One Number

Count digits, sum iPow(d, k) for each digit, then compare with the original value.

C++
#include <iostream>

int iPow(int baseVal, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= baseVal;
    }
    return result;
}

bool isArmstrong(int n) {
    if (n <= 0) {
        return false;
    }

    int power = 0;
    for (int t = n; t > 0; t /= 10) {
        power++;
    }

    int total = 0;
    int temp = n;
    while (temp > 0) {
        int digit = temp % 10;
        total += iPow(digit, power);
        temp /= 10;
    }

    return total == n;
}

int main() {
    int number = 153;

    if (isArmstrong(number)) {
        std::cout << number << " is an Armstrong number.\n";
    } else {
        std::cout << number << " is not an Armstrong number.\n";
    }

    return 0;
}

How It Works

  1. Return false for non-positive inputs.
  2. Count digits into power with a working copy.
  3. Peel digits via temp and add iPow(digit, power) each time.
  4. Compare total with the original n.
  5. iPow stays in integer math — no floating std::pow.

Example 2 — Armstrong Numbers from 1 to 200

Reuse the helper and print every match in a closed range.

C++
#include <iostream>

int iPow(int baseVal, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= baseVal;
    }
    return result;
}

bool isArmstrong(int n) {
    if (n <= 0) {
        return false;
    }

    int power = 0;
    for (int t = n; t > 0; t /= 10) {
        power++;
    }

    int total = 0;
    int temp = n;
    while (temp > 0) {
        int digit = temp % 10;
        total += iPow(digit, power);
        temp /= 10;
    }

    return total == n;
}

int main() {
    int start = 1;
    int end = 200;

    std::cout << "Armstrong numbers in the range " << start
              << " to " << end << ":\n";
    for (int value = start; value <= end; value++) {
        if (isArmstrong(value)) {
            std::cout << value << " ";
        }
    }
    std::cout << "\n";
    return 0;
}

How It Works

  1. Loop from start to end inclusive.
  2. Call isArmstrong(value) for each value.
  3. Single-digit numbers print first (each is Armstrong).
  4. 153 is the only other hit in 1…200.
  5. The helper stays pure; the loop only decides what to print.

Example 3 — String-Based Digit Walk

Convert to a string, raise each character digit to power s.size(), and compare.

C++
#include <iostream>
#include <string>

int iPow(int baseVal, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= baseVal;
    }
    return result;
}

bool isArmstrong(int n) {
    if (n <= 0) {
        return false;
    }

    std::string s = std::to_string(n);
    int power = (int) s.size();
    int total = 0;

    for (size_t i = 0; i < s.size(); i++) {
        int digit = s[i] - '0';
        total += iPow(digit, power);
    }

    return total == n;
}

int main() {
    std::cout << (isArmstrong(153) ? "true" : "false") << "\n";
    std::cout << (isArmstrong(123) ? "true" : "false") << "\n";
    return 0;
}

How It Works

  1. Convert n with std::to_string so digit count is s.size().
  2. Walk each character and convert with s[i] - '0'.
  3. Add iPow(digit, power) for every digit.
  4. 153 prints true; 123 prints false.
  5. Readable C++ style; interviews often still prefer the arithmetic peel.

Edge Cases & Pitfalls

Check these before calling the solution done.

Exponent

Use digit count k

Do not hard-code cubes unless the problem is only about 3-digit numbers.

std::pow

Prefer integer powers

Floating pow can round wrong. Use a small integer iPow helper.

Zero

Definitions vary

This page returns false for n <= 0. Follow the problem statement if it includes 0.

Overflow

Use a wider sum

For large n, accumulate with long long so digit powers do not wrap.

Time and Space Complexity

ProgramTimeExtra space
Single checkO(log n)O(1)
Range of size NO(N log N)O(1)
String digit walkO(log n)O(log n) for the string

Digit count d is about log₁₀ n. Each power costs O(d) with a simple loop, still fine for interviews.

Key Takeaways

  • Definition: sum of each digit raised to the digit count equals n.
  • Classic: 153 = 1³ + 5³ + 3³.
  • Powers: use integer iPow; avoid floating std::pow.
  • Test set: 153, 370, 9 yes; 123 no.

One line: raise every digit to the digit count — if the sum equals n, it is Armstrong.

Frequently Asked Questions

A positive integer n is Armstrong (narcissistic) if it equals the sum of its digits, each raised to the power of the digit count. Example: 153 has 3 digits and 1³ + 5³ + 3³ = 153.
Yes. For a one-digit n, the count is 1 and n¹ = n. This page treats n <= 0 as not Armstrong.
Floating-point pow can round incorrectly. Integer exponentiation stays exact for typical interview sizes.
The same k for every digit, where k is the number of digits in n. Do not hard-code cubes unless you only care about 3-digit cases.
Counting digits and summing powers are both O(d), and d is about log₁₀ n, so one check is O(log n).

Next: Automorphic Number

Check whether a number’s square ends with the same digits.

Automorphic number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful