C++ Program to Check Armstrong Number

11/04/2024 0 By indiafreenotes

An Armstrong number, also known as a narcissistic number, is an intriguing concept in number theory that finds practical applications in programming exercises, particularly those focusing on algorithm design, control structures, and number manipulation. An Armstrong number of a given number of digits is a number in which the sum of its own digits each raised to the power of the number of digits equals the number itself. For example, 153 is an Armstrong number because 5^3+3^3 = 153.

Understanding and implementing a program to check for Armstrong numbers not only helps in grasping basic programming concepts but also in appreciating the elegance of numerical properties. This exercise typically involves breaking down the problem into key steps: determining the number of digits, calculating the sum of the digits each raised to a certain power, and comparing this sum to the original number.

Breakdown of the Solution

  1. Determine the Number of Digits:

This involves a loop where you repeatedly divide the number by 10 until it becomes 0. The number of iterations gives you the number of digits.

  1. Calculate the Sum of Powers of the Digits:

Using the number of digits calculated in step 1, you raise each digit to this power and sum these values. This requires isolating each digit (using modulus and division operations) and then using the pow function from the <cmath> library to raise each digit to the power of the number of digits.

  1. Compare the Sum to the Original Number:

If the sum obtained in step 2 equals the original number, it is an Armstrong number.

Implementing the Program

Let’s now translate the solution into a C++ program:

#include <iostream>

#include <cmath> // For pow() function

using namespace std;

// Function to calculate the number of digits in the number

int countDigits(int n) {

    int count = 0;

    while (n != 0) {

        count++;

        n /= 10;

    }

    return count;

}

// Function to check if the given number is an Armstrong number

bool isArmstrong(int num) {

    int originalNum = num;

    int numOfDigits = countDigits(num);

    int sumOfPowers = 0;

    while (num != 0) {

        int digit = num % 10;

        sumOfPowers += pow(digit, numOfDigits);

        num /= 10;

    }

    return sumOfPowers == originalNum;

}

int main() {

    int num;

    // Prompt the user for input

    cout << “Enter a number: “;

    cin >> num;

    if (isArmstrong(num)) {

        cout << num << ” is an Armstrong number.” << endl;

    } else {

        cout << num << ” is not an Armstrong number.” << endl;

    }

    return 0;

}

 

Detailed Explanation

  1. Counting Digits (countDigits function):

This function takes an integer as input and returns the count of digits in that number. It does so by dividing the number by 10 in a loop until the number becomes 0, incrementing a counter at each step.

  1. Checking for Armstrong Number (isArmstrong function):

The isArmstrong function assesses whether a given number is an Armstrong number. It first stores the original number for later comparison. It then calculates the sum of each digit raised to the power equal to the number of digits in the number. This is accomplished by isolating each digit using modulus and division operations and raising it to the power calculated by the countDigits function.

  1. Main Logic:

The main function prompts the user for a number, uses the isArmstrong function to check if the number is an Armstrong number, and displays the result accordingly.