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.
* C++ Program to Print all the Armstrong Numbers
int sum, val;
std::cout << "Armstrong numbers => ";
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
for(int k = 0; k < 10; k++)
val = i * 100 + j * 10 + k;
sum = pow(i, 3) + pow(j, 3) + pow(k, 3);
if(val == sum)
std::cout << val << " ";
std::cout << std::endl;
Armstrong numbers => 0 1 153 370 371 407
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
If you wish to look at all C++ Programming examples, go to C++ Programs
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1,
- Chapterwise Mock Tests: Chapter 1,
, 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