A C++ program to compute and display all Armstrong numbers (numbers such that the sum of each of the digits raised to the power of the number of digits equals the number) between 100 and 999. The answers should be 153, 370, 371, 407. It prints all but 153.
Debugging done to see what the values of the individual digits are for i = 153.
#include <iostream>
#include <vector>
#include <math.h>
void separateDigits(int n, std::vector<int>& digits)
{
if (n>0)
{
separateDigits(n/10, digits);
digits.push_back(n%10);
}
}
int main()
{
for (int i = 100; i <= 999; i++)
{
std::vector<int> test;
separateDigits(i, test);
int powerSum = 0;
for (auto iter = test.begin(); iter != test.end(); iter++)
{
//powerSum = powerSum + pow((*iter),3);
powerSum = powerSum + (*iter)*(*iter)*(*iter);
}
if (i==powerSum)
{
std::cout << "Armstrong: " << i << std::endl;
}
}
return 0;
}
#include <stdio.h> //PROGRAM TO FIND ARMSRONG NUMBERS.
#include <math.h> //WORKS ONLY FOR THREE DIGIT NUMBERS.
int main()
{
int num, dig1, dig2, dig3;
for (num = 1; num <= 999; num++)
{
dig3 = dig2 = dig1 = 0;
dig3 = num % 10;
dig2 = ((num - dig3) % 100) / 10;
dig1 = (num - (dig3 + 10 * dig2)) / 100;
if (num == pow(dig1, 3) + pow(dig2, 3) + pow(dig3, 3))
{
printf("%d\n", num);
}
}
return 0;
}