C++ Program to Check Armstrong Number

In this post, we will learn how to check Armstrong numbers using C++ Programming language. Let’s first learn about Armstrong numbers.

A number is called an Armstrong number if the sum of cubes of its digit is equal to the original number. For example, 370 is an Armstrong number because (3)3 + (7)3 + (0)3 = 27 + 343 + 0 = 370.

We will check whether the sum is equal to the original number or not. If the sum of cubes of its digits is equal to the original number, then it is an Armstrong number.

So, without further ado, let’s begin this tutorial.

C++ Program to Check Armstrong Number

// C++ Program to Check Armstrong Number
#include <iostream>
using namespace std;

int main(){
    int n, rem, sum = 0;
    
    // Asking for input
    cout << "Enter a positive integer: ";
    cin >> n;
    
    int temp = n;
    
    // Calculating the sum
    while (temp != 0){
        rem = temp % 10;
        sum = sum + (rem * rem * rem);
        temp = temp / 10;
    }
    
    // Checking armstrong number
    if(n == sum){
        cout << n << " is an Armstrong number." << endl;
    }
    else {
        cout << n << " is not an Armstrong number." << endl;
    }
    return 0;
}

Output 1

Enter a positive integer: 153
153 is an Armstrong number.

Output 2

Enter a positive integer: 145
145 is not an Armstrong number.

How Does This Program Work ?

    int n, rem, sum = 0;

In this program, we have declared three integer data type variables named n, rem and sum.

    cout << "Enter a positive integer: ";
    cin >> n;

The user is asked to enter a positive integer. This value gets stored in the ‘n’ named variable.

    // Calculating the sum
    while (temp != 0){
        rem = temp % 10;
        sum = sum + (rem * rem * rem);
        temp = temp / 10;
    }

We calculate the sum of cubes of its digit using for loop. Suppose the user enters a number 153, then:

  • 1st While Loop Iteration: (153 != 0), here the condition is True, so the loop continues.
    • rem = n % 10 = 153 % 10 = 3
    • sum = sum + (rem * rem * rem) = 0 + (3*3*3) = 0 + 27 = 27.
    • temp = temp / 10 = 153 / 10 = 15
  • 2nd While Loop Iteration: (15 != 0), here the condition is True, so the loop continues.
    • rem = n % 10 = 15 % 10 = 5
    • sum = sum + (rem * rem * rem) = 27 + (5*5*5) = 27 + 125 = 152.
    • temp = temp / 10 = 15 / 10 = 1
  • 3rd While Loop Iteration: (1 != 0), here the condition is True, so the loop continues.
    • rem = n % 10 = 1 % 10 = 1
    • sum = sum + (rem * rem * rem) = 152 + (1*1*1) = 152 + 1 = 153.
    • temp = temp / 10 = 1 / 10 = 0
  • 4th While Loop Iteration: (0 != 0), here the condition is False, so the loop terminates and we get sum = 153.
    // Checking armstrong number
    if(n == sum){
        cout << n << " is an Armstrong number." << endl;
    }
    else {
        cout << n << " is not an Armstrong number." << endl;
    }

Now, we check whether the sum is equal to the original number or not. If yes, then the entered integer is an Armstrong number otherwise the entered integer is not an Armstrong number.

Conclusion

I hope after going through this post, you understand how to check an Armstrong number using C++ Programming language.

If you have any problem regarding the program, then contact us in the comment section. We will be delighted to assist you. 

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *