C++ Program to Check Armstrong Number

«
»

This is a C++ Program to Check if a Number is an Armstrong Number.

Problem Description

The program takes a number and checks if it is an 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.

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.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int num, temp, rem, sum = 0;
  6.     cout << "Enter number to be checked : ";
  7.     cin >> num;
  8.     temp = num;
  9.     while (temp != 0)
  10.     {
  11.         rem = temp % 10;
  12.         sum = sum + rem*rem*rem;
  13.         temp = temp / 10;
  14.     }
  15.     if (sum == num)
  16.         cout << "\n" << num << " is an Armstrong number.";
  17.     else
  18.         cout << "\n" << num << " is not an Armstrong number.";
  19.     return 0;
  20. }
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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter number to be checked : 371
371 is an Armstrong number.
 
Case 2 :
Enter number to be checked : 18
18 is not an Armstrong number.
 
Case 3 :
Enter number to be checked : 153
153 is an Armstrong number.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

advertisement
advertisement
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 & technical discussions at Telegram SanfoundryClasses.