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
*/
#include <iostream>
#include <cmath>
int main()
{
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.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check C++ Books
- Check Programming Books
- Apply for C++ Internship
- Check Computer Science Books
- Practice Programming MCQs