C++ Program to Check Armstrong Number

Updated on December 20, 2024
Check armstrong number header image

Introduction

An Armstrong number (also known as 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 it has 3 digits, and if you take each digit raised to the power of 3 and sum them, you get 153 (1^3 + 5^3 + 3^3 = 153). This concept is frequently explored in computer science education to teach loops, conditionals, and the manipulation of numbers in various programming languages including C++.

In this article, you will learn how to craft a C++ program to identify if a number is an Armstrong number. Mastery of this task strengthens your grasp of fundamental C++ operations, conditional logic, and the usage of mathematical functions. Various examples will elucidate the step-by-step process to build an effective program.

Understanding the Armstrong Number Checking Concept

Breaking Down the Logic

  1. Calculate the number of digits in the given number.
  2. Sum each digit of the number raised to the power of the total digits count.
  3. Compare the resulting sum with the original number.

Verifying if a Single Number is an Armstrong Number

  1. Assume the number 153 which you need to verify.

  2. Analyze the steps to perform the verification:

    cpp
    #include <iostream>
    #include <cmath> // for pow function
    
    bool isArmstrong(int num) {
        int originalNum = num, sum = 0, digits = 0;
    
        // Counting the number of digits
        for (int n = num; n > 0; n /= 10) {
            digits++;
        }
    
        // Calculating the sum of digits raised to the power of the count of digits
        for (int n = num; n > 0; n /= 10) {
            int digit = n % 10;
            sum += pow(digit, digits);
        }
    
        // Returning true if the sum of digits^digits == original num
        return sum == originalNum;
    }
    
    int main() {
        int num = 153;
        if (isArmstrong(num)) {
            std::cout << num << " is an Armstrong number." << std::endl;
        } else {
            std::cout << num << " is not an Armstrong number." << std::endl;
        }
    }
    

    This code snippet defines a function isArmstrong() that implements the logic for checking an Armstrong number. Notice the loop for digit extraction and the power computation, which are pivotal in the calculation.

Exploring Various Cases

  1. It's beneficial to test the program with multiple examples, including edge cases like single-digit numbers and large numbers.

  2. Integrate additional examples directly into your main function or through unit testing.

    cpp
    int main() {
        // Examples to test the isArmstrong function
        int testNumbers[] = {1, 153, 370, 371, 9474, 9475};
        for (int num : testNumbers) {
            if (isArmstrong(num)) {
                std::cout << num << " is an Armstrong number." << std::endl;
            } else {
                std::cout << num << " is not an Armstrong number." << std::endl;
            }
        }
    }
    

    This script tests numbers from the array testNumbers to check which ones are Armstrong numbers. The output helps verify the accuracy of our function across a spectrum of numbers.

Conclusion

The task of determining whether a number is an Armstrong number is more than an exercise in using mathematical operations—it also empowers you with deeper insights into the control structures within C++. By following the steps outlined and experimenting with the provided code examples, you get a robust introduction to combining iteration, conditional evaluation, and mathematical functions in C++. As you develop more complex programs, the essential skills demonstrated here ensure your code remains efficient and your logical implementation sound.