C++ Program to Check Armstrong Number

Problem Description

Write a C++ program to check if a number is an Armstrong number. If the number is an Armstrong number, display ‘It is an Armstrong number’ otherwise, display ‘It is not an Armstrong number’.

What is Armstrong number?

An n-digit number such that the sum of each digit raised to the power n is the number itself, is an Armstrong number.

Let’s take 371 as an example to understand why it is an Armstrong number.

371 = 3^3 + 7^3 + 1^3
    = 27 + 343 + 1
    = 371
Problem Solution

1. The number to be checked is entered.
2. Each digit is cubed, and added.
3. If the eventual sum of the cubed digits is equal to the number entered, then it is an Armstrong number.
4. Else it is not.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Check if a Number is an Armstrong Number. The program output is shown below.

 * C++ Program to Check whether a given Number is Armstrong
using namespace std;
int main ()
    int num, temp, rem, sum = 0;
    cout << "Enter number to be checked : ";
    cin >> num;
    temp = num;
    while (temp != 0)
        rem = temp % 10;
        sum = sum + rem*rem*rem;
        temp = temp / 10;
    if (sum == num)
        cout << "\n" << num << " is an Armstrong number.";
        cout << "\n" << num << " is not an Armstrong number.";
    return 0;
Program Explanation

1. The user is asked to enter the number to be checked and it is stored in the variable ‘num‘.
2. The variable sum is initialized as 0.
3. The value of num is assigned to a temporary variable temp and it is checked.
4. Each digit is cubed and added to the variable sum using a while loop and modulus operator.
5. If the value of sum is equal to the value of the number entered, i.e num, then it is an Armstrong number.
6. Else the entered number is not an Armstrong number.
7. The result is then printed.

Time complexity: O(log n)
The time complexity of this program is O(log n), where n is the number entered by the user. The while loop iterates based on the number of digits in the input number.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Space complexity: O(1)
The space complexity is O(1) since the program uses a fixed amount of memory to store variables, regardless of the input size.

Runtime Test Cases

Testcase 1: In this case, we enter the number “371” as input to check whether it is a armstrong number or not.

Enter number to be checked : 371
371 is an Armstrong number.

Testcase 2: In this case, we enter the number “18” as input to check whether it is a armstrong number or not.

Enter number to be checked : 18
18 is not an Armstrong number.

Testcase 3: In this case, we enter the number “153” as input to check whether it is a armstrong number or not.

Enter number to be checked : 153
153 is an Armstrong number.

To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.