C++ Program to check armstrong number or not


January 19, 2023, Learn eTutorial
1294

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?

According to the numerics, Armstrong number is defined as a number in which the sum of its digits raised to its position number will be the same as the original number.
Example:  370 = 33+73+03 = 3*3*3 + 7*7*7 + 0*0*0 =  27 + 343 + 0 = 370

A number of n digits can be called an Armstrong number if abcd...=an+bn+cn+dn+... 

where,

  • a,b,c,d... are the digits of the number
  • n is the count of digits of the number.

To understand more about the concepts of Armstrong Numbers, we assure you our blog The basics of Armstrong numbers will help you very much.

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; We get the last digit of the number 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. For removing the last digit from the original_number perform the operation original_number /= 10. and then continue the loop. 

Finally, the variable result contains the sum of the cubes of each digit. Now we have to check whether it is an Armstrong number or not. This can be performed by using a if-else statement. If result = number then the 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 display it is not an Armstrong.

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.