C++ Program to Print Armstrong Number between 1 to 1000

This C++ program prints all the armstrong numbers upto 1000. Armstrong numbers are those numbers whose value is equal to the sum of digits of the number to their third power. The nested loop are used for this purpose, where index of each loop is used as one digit of the number.

The number is computed using these indices inside the innermost loop and the is compared with the value obtained by raising each digit of the number to its third power and summing it. If these values are equal, the number is an armstrong number and is printed on the standard output.

Here is the source code of the C++ program prints all the armstrong numbers upto 1000. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Print all the Armstrong Numbers
  3.  */ 
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7. int main()  
  8. {
  9.     int sum, val;
  10.  
  11.     std::cout << "Armstrong numbers => ";
  12.     for(int i = 0; i < 10; i++)
  13.     {
  14.         for(int j = 0; j < 10; j++)
  15.         {
  16.             for(int k = 0; k < 10; k++)
  17.             {
  18.                 val = i * 100 + j * 10 + k;
  19.                 sum = pow(i, 3) + pow(j, 3) + pow(k, 3);
  20.                 if(val == sum)
  21.                     std::cout << val << "  ";
  22.             }
  23.         }
  24.     }
  25.     std::cout << std::endl;
  26. }

$ a.out
Armstrong numbers => 0  1  153  370  371  407

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

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

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