C++ Program To Check Armstrong Number

Last Updated : 18 Aug, 2026

An Armstrong number is a number that is equal to the sum of its digits, each raised to the power of the total number of digits.

  • Armstrong numbers are also called Narcissistic numbers.
  • The power used for each digit is equal to the number of digits in the number.

Examples

Input: 153
Output: Yes
Explanation: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, so 153 is an Armstrong number.

Input: 120
Output: No
Explanation: 1³ + 2³ + 0³ = 1 + 8 + 0 = 9, which is not equal to 120.

Input: 1634
Output: Yes
Explanation: 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634, so 1634 is an Armstrong number.

Methods to Check an Armstrong Number

An Armstrong number can be checked by first counting its digits and then calculating the sum of the required powers of its digits.

Method 1: Using Digit Count and Power Calculation

In this approach, we first count the number of digits in the given number. Then, each digit is extracted and raised to the power of the total number of digits. If the resulting sum is equal to the original number, the number is an Armstrong number.

Approach

  • Count the number of digits in the given number.
  • Extract each digit using the modulo operator %.
  • Raise each digit to the power of the number of digits.
  • Add all the calculated values.
  • Compare the sum with the original number.
C++
#include <iostream>
using namespace std;

bool isArmstrong(int n)
{
    int original = n;
    int digits = 0;
    int sum = 0;

    // Count the number of digits
    int temp = n;
    while (temp > 0) {
        digits++;
        temp /= 10;
    }

    // Calculate the sum of powers of digits
    temp = n;

    while (temp > 0) {
        int digit = temp % 10;

        int power = 1;
        for (int i = 0; i < digits; i++)
            power *= digit;

        sum += power;
        temp /= 10;
    }

    return sum == original;
}

int main()
{
    int n = 153;

    if (isArmstrong(n))
        cout << "Yes";
    else
        cout << "No";

    return 0;
} 

Output
Yes

Explanation: For n = 153:

  • The number contains 3 digits.
  • Extract the digits 3, 5, and 1.
  • Calculate 3³ + 5³ + 1³.
  • The sum is 27 + 125 + 1 = 153.
  • Since the sum is equal to the original number, 153 is an Armstrong number.

Method 2: Using pow() Function

The power calculation can be simplified using the pow() function from the <cmath> header. The remaining logic remains the same.

C++
#include <cmath>
#include <iostream>
using namespace std;

bool isArmstrong(int n)
{
    int original = n;
    int digits = 0;
    int sum = 0;

    // Count the number of digits
    int temp = n;

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

    // Calculate the sum of powers of digits
    temp = n;

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

    return sum == original;
}

int main()
{
    int n = 153;

    cout << (isArmstrong(n) ? "Yes" : "No");

    return 0;
}

Output
Yes

Finding Armstrong Numbers in a Range

The same Armstrong number check can be used to find all Armstrong numbers within a given range.

Approach

  • Traverse all numbers from the lower to the upper limit.
  • Check each number using the Armstrong number condition.
  • Print the number if it satisfies the condition.
C++
#include <cmath>
#include <iostream>
using namespace std;

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

    int original = n;
    int digits = 0;
    int sum = 0;

    int temp = n;

    if (n == 0)
        digits = 1;

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

    temp = n;

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

    return sum == original;
}

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

    for (int i = start; i <= end; i++) {
        if (isArmstrong(i))
            cout << i << " ";
    }

    return 0;
} 

Output
1 2 3 4 5 6 7 8 9 153 370 371 407 

Finding the Nth Armstrong Number

The Nth Armstrong number can be found by checking consecutive numbers and counting those that satisfy the Armstrong condition.

Approach

  • Start checking numbers from 0 or 1.
  • Check each number using the Armstrong condition.
  • Increment the count whenever an Armstrong number is found.
  • Return the number when the count reaches n.
C++
#include <cmath>
#include <iostream>
using namespace std;

bool isArmstrong(int n)
{
    int original = n;
    int digits = 0;
    int sum = 0;

    int temp = n;

    if (n == 0)
        digits = 1;

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

    temp = n;

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

    return sum == original;
}

int main()
{
    int n = 12;
    int count = 0;

    for (int i = 0; ; i++) {
        if (isArmstrong(i)) {
            count++;

            if (count == n) {
                cout << i;
                break;
            }
        }
    }

    return 0;
} 
Try It Yourself
redirect icon

Output
370
Comment