BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to check Armstrong Number

Last Updated: September 7, 2017 by Chaitanya Singh | Filed Under: C++ Programs

An integer number is called Armstrong number if sum of the cubes of its digits is equal to the number itself. For example:
370 is an armstrong number because:

370 = 3*3*3 + 7*7*7 + 0*0*0
    = 27 + 343 + 0
    = 370

Example: Check Armstrong Number using For loop

To understand this program, you should have the knowledge of for loop and if-else statement.

#include <iostream>
using namespace std;

int main() {
   int num, sum = 0, digit;
   cout<<"Enter a positive integer: ";
   cin>>num;

   for(int temp=num; temp!=0;){
      digit = temp % 10;
      sum = sum +(digit * digit * digit);
      temp = temp/10;
   }

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

   return 0;
}

Output:

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

You can also use while loop instead of for loop to check the Armstrong number:
Replace this part of the code:

for(int temp=num; temp!=0;){
    digit = temp % 10;
    sum = sum +(digit * digit * digit);
    temp = temp/10;
}

with this:

int temp = num;
while(temp != 0)
{
   digit = temp % 10;
   sum = sum +(digit * digit * digit);
   temp = temp/10;
}

Top Related Articles:

  1. C++ Program to check whether the input number is Even or Odd
  2. C++ Program to add two numbers
  3. C++ Program to Check Armstrong Number using user-defined function
  4. C++ Program to find the sum of n natural numbers
  5. C++ Program to Display the Number Entered by User

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap