Armstrong Numbers
Completed
Read a and b (≤ 100000) and print all Armstrong numbers in [a, b] (a number equal to the sum of its digits each raised to the number of digits), or none.
Input
Two integers.
Output
One line.
Example
Input
100 999
Output
153 370 371 407
Editor
C
Hints
Count the digits first, then add each digit to that power.
Solution explanation
153 = 1³ + 5³ + 3³ is the smallest three-digit Armstrong number.
Show solution
#include <stdio.h>
int main() {
int a, b, found = 0;
scanf("%d %d", &a, &b);
for (int x = a; x <= b; x++) {
int digits = 0, t = x;
do { digits++; t /= 10; } while (t > 0);
int sum = 0;
t = x;
do {
int d = t % 10, p = 1;
for (int i = 0; i < digits; i++) p *= d;
sum += p;
t /= 10;
} while (t > 0);
if (sum == x) {
if (found) printf(" ");
printf("%d", x);
found = 1;
}
}
printf(found ? "\n" : "none\n");
return 0;
}