C++ Program to Check Armstrong Number

C++ Program to Check Armstrong Number

An Armstrong number, also called a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because $1^3 + 5^3 + 3^3 = 153$. Checking for Armstrong numbers is a common exercise in C++ that helps beginners practice loops, arithmetic operations, and modular programming. In this article, we will explore multiple approaches to check Armstrong numbers in an easy-to-understand manner.

Program 1: Basic Method to Check Armstrong Number

The simplest way to check an Armstrong number is to extract each digit, raise it to the power of the total digits, sum them, and compare with the original number.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    int num, originalNum, remainder, digits = 0;
    double sum = 0.0;

    cout << "Enter a number: " << endl;
    cin >> num;

    originalNum = num;

    // Count the number of digits
    while(originalNum != 0) {
        originalNum /= 10;
        digits++;
    }

    originalNum = num;

    // Calculate the sum of digits raised to the power of total digits
    while(originalNum != 0) {

        remainder = originalNum % 10;
        sum += pow(remainder, digits);
        originalNum /= 10;

    }

    if(static_cast<int>(sum) == num)
        cout << num << " is an Armstrong number." << endl;

    else
        cout << num << " is not an Armstrong number." << endl;

    return 0;

}

In this program, we first count the number of digits. Then, each digit is raised to that power and summed. Comparing the sum with the original number determines if it is an Armstrong number. This method is straightforward and helps beginners understand basic loops, modular arithmetic, and the pow() function.

Program 2: Using a Function to Check Armstrong Number

Encapsulating the logic in a function makes the code modular, reusable, and easier to maintain, especially when checking multiple numbers.

#include <iostream>
#include <cmath>

using namespace std;

bool isArmstrong(int num) {

    int originalNum = num, remainder, digits = 0;
    double sum = 0.0;

    while(originalNum != 0) {
        originalNum /= 10;
        digits++;
    }

    originalNum = num;

    while(originalNum != 0) {

        remainder = originalNum % 10;
        sum += pow(remainder, digits);
        originalNum /= 10;

    }

    return static_cast<int>(sum) == num;

}

int main() {

    int num;

    cout << "Enter a number: " << endl;
    cin >> num;

    if(isArmstrong(num))
        cout << num << " is an Armstrong number." << endl;

    else
        cout << num << " is not an Armstrong number." << endl;

    return 0;

}

By using the isArmstrong() function, the program separates the logic for checking Armstrong numbers from input/output handling. Beginners can see how functions improve code clarity and allow easy reuse for multiple numbers or arrays of numbers.

Program 3: Checking Armstrong Number in a Range

Sometimes it is useful to check all Armstrong numbers within a given range. This approach applies the same logic iteratively for each number.

#include <iostream>
#include <cmath>

using namespace std;

bool isArmstrong(int num) {

    int originalNum = num, remainder, digits = 0;
    double sum = 0.0;

    while(originalNum != 0) {
        originalNum /= 10;
        digits++;
    }

    originalNum = num;

    while(originalNum != 0) {

        remainder = originalNum % 10;
        sum += pow(remainder, digits);
        originalNum /= 10;

    }

    return static_cast<int>(sum) == num;

}

int main() {

    int start, end;

    cout << "Enter the start and end of the range: " << endl;
    cin >> start >> end;

    cout << "Armstrong numbers in the range are: ";

    for(int i = start; i <= end; i++) {

        if(isArmstrong(i))
            cout << i << " ";

    }

    cout << endl;

    return 0;

}

This program highlights how modular code can be extended to handle ranges of numbers. Beginners learn how loops and functions can be combined to solve more complex problems efficiently.

Program 4: Optimized Armstrong Check Without pow()

Using the pow() function introduces floating-point operations, which can be slower and less precise for integers. Instead, we can calculate powers manually using integer multiplication, improving performance and avoiding rounding errors.

#include <iostream>

using namespace std;

// Function to calculate integer power
int intPower(int base, int exp) {

    int result = 1;

    for(int i = 0; i < exp; i++)
        result *= base;

    return result;

}

bool isArmstrong(int num) {

    int originalNum = num, remainder, digits = 0, sum = 0;

    // Count digits
    while(originalNum != 0) {

        originalNum /= 10;
        digits++;

    }

    originalNum = num;

    // Sum powers of digits
    while(originalNum != 0) {

        remainder = originalNum % 10;
        sum += intPower(remainder, digits);
        originalNum /= 10;

    }

    return sum == num;

}

int main() {

    int num;

    cout << "Enter a number: " << endl;
    cin >> num;

    if(isArmstrong(num))
        cout << num << " is an Armstrong number." << endl;

    else
        cout << num << " is not an Armstrong number." << endl;

    return 0;

}

In this program, the intPower() function multiplies the digit by itself repeatedly instead of using floating-point pow(). This keeps all calculations in integers, improving precision and speed. Beginners can see how breaking down mathematical operations into basic steps can enhance performance while maintaining clarity.

Program 5: Print All Armstrong Numbers in a Range Without pow()

Instead of checking a single number, we can iterate through a range and print all Armstrong numbers. Using integer multiplication avoids floating-point rounding issues and keeps the program fast.

#include <iostream>

using namespace std;

// Function to calculate integer power
int intPower(int base, int exp) {

    int result = 1;

    for(int i = 0; i < exp; i++)
        result *= base;

    return result;

}

// Function to check if a number is Armstrong
bool isArmstrong(int num) {

    int originalNum = num, remainder, digits = 0, sum = 0;

    // Count number of digits
    while(originalNum != 0) {
        originalNum /= 10;
        digits++;
    }

    originalNum = num;

    // Sum the digits raised to the power of the number of digits
    while(originalNum != 0) {

        remainder = originalNum % 10;
        sum += intPower(remainder, digits);
        originalNum /= 10;

    }

    return sum == num;

}

int main() {

    int start, end;

    cout << "Enter the range (start and end): " << endl;
    cin >> start >> end;

    cout << "Armstrong numbers between " << start << " and " << end << " are: ";

    for(int i = start; i <= end; i++) {

        if(isArmstrong(i))
            cout << i << " ";

    }

    cout << endl;

    return 0;

}

This program introduces beginners to modular functions, loops, and range processing. The isArmstrong() function is reused for each number in the range, demonstrating clean, reusable code. By avoiding pow(), we ensure integer precision and better performance, even for larger ranges.

With this approach, learners can explore Armstrong numbers in any range and understand how to efficiently process multiple numbers using functions.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about Armstrong numbers in C++:

Q1: What is an Armstrong number?
An Armstrong number is equal to the sum of its own digits each raised to the power of the number of digits.

Q2: Can a single-digit number be an Armstrong number?
Yes, any single-digit number is considered an Armstrong number because it equals itself raised to the power of one.

Q3: Is 0 an Armstrong number?
Yes, 0 is an Armstrong number by definition.

Q4: Can I use this program for large numbers?
Yes, but for very large numbers, consider using long long instead of int to prevent overflow.

Q5: Why use a function to check Armstrong numbers?
Functions make the code modular, reusable, and easier to read, which is especially useful for multiple numbers or ranges.

Conclusion

Checking Armstrong numbers in C++ teaches beginners about loops, modular arithmetic, and function design. We explored methods including a simple loop, a reusable function, and checking numbers within a range. Practicing these techniques strengthens understanding of programming logic and mathematical concepts, preparing beginners for more advanced number-related problems.

Additional & References

Understanding Armstrong numbers provides a foundation for working with digits, powers, and number properties. Beginners are encouraged to try generating Armstrong numbers in ranges, experimenting with large numbers, and combining the logic with arrays or user inputs.

Scroll to Top