C++ Program to check armstrong number or not

In this example, you will learn to check whether a user entered 3 digit number is an Armstrong number or not.

What is an Armstrong number?

 In numerical number theory, the Armstrong number definition is the number in any given number base, which forms the total of the same number, when each of its digits is raised to the power of the number of digits in the number.
Example:  370 = 3*3*3 + 7*7*7 + 0*0*0 =  27 + 343 + 0 = 370

How will you check for an Armstrong number ( 3 digits )?

We can check for an Armstrong number by checking whether the entered three-digit number is an integer such that the sum of the cubes of its digits is equal to the number itself.
158 =! 1*1*1 + 5*5*5 + 8*8*8 not an Armstrong number
153 = 1*1*1 + 5*5*5 + 3*3*3 an Armstrong number

How to check Armstrong number of 3 digits in C++?

Ask the user to enter a three-digit integer and read the number into the integer type variable number. Copy the entered number into another variable original_number.  Here, we iterate through the while loop until the original_number is 0.
While ( original_number != 0 ). Declare another integer variable remainder to get each digit of the number.
remainder = original_number % 10; the last digit of the number can be gotten from this operation.
Initialize a variable result with 0 and add the cube of the separated digit of the number and update the value of the variable result.
result += remainder * remainder * remainder ;
For removing the last digit from the original_number perform the operation original_number /= 10;
Finally the variable result contains the sum of the cubes of each digits.
While ( original_number != 0 )
{ remainder = original_number % 10;
  result += remainder * remainder * remainder;
  Original_number /= 10; }

Now we have to check whether it is an Armstrong number or not.
This can be performed by using an if-else statement
If result = number then print number is an Armstrong number
Else print number is not an Armstrong number. 

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variable number, original_number, remainder, result.

Step 5: Initialise the value of the variable result to 0; result = 0

Step 6: Print a message to enter a three-digit integer.

Step 7: read the integer into the variable number

Step 8: Copy the value of the number into the variable original_number;

Step 9: get the last digit of the number on the variable remainder by the equation                           original_number % 10.

Step 10: add the cube of the last digit with the variable result.

Step 11: remove the last digit from the original number and get the sum of the cubes of                      the digits of the number. original_number/=10

Step 12: continue step 9- 11 until original_number = 0

Step 13:  check if the result is equal to the number itself

Step 14: If yes then the print number is an Armstrong number

Step 15: Else print not an Armstrong number.

Step 16: Exit
 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    int number, original_number, remainder, result = 0;
    cout << "Enter a three-digit integer: ";
    cin >> number;
    original_number = number;

    while (original_number != 0) {
        // remainder contains the last digit
        remainder = original_number % 10;
        
        result += remainder * remainder * remainder;
        
        // removing last digit from the orignal number
        original_number /= 10;
    }

    if (result == number)
        cout << number << " is an Armstrong number.";
    else
        cout << number << " is not an Armstrong number.";

    return 0;
}

                                      

OUTPUT

RUN 1
Enter a three-digit integer: 407
407 is an Armstrong number.
RUN 2
Enter a three-digit integer: 347
347 is not an Armstrong number.