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;
$ a.out Armstrong numbers => 0 1 153 370 371 407
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.