C Program to Print Armstrong Numbers From 1 to 1000
C Program to Print Armstrong Numbers From 1 to 1000
A number puzzle compares a student’s chosen number with a sum computed from its decimal digits. The digit count must determine the power, rather than always cubing each digit.
An Armstrong number equals the sum of its decimal digits each raised to the number of decimal digits in the original number.
Before this exercise, review integer division and remainders and loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why armstrong numbers 1–1000 uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use the changed interval shown in the worked answer and predict its matching values.
Armstrong Numbers 1–1000: follow the data
Given data
A number puzzle compares a student’s chosen number with a sum computed from its decimal digits. The digit count must determine the power, rather than always cubing each digit.
Rule
An Armstrong number equals the sum of its decimal digits each raised to the number of decimal digits in the original number.
Observed result
1 2 3 4 5 6 7 8 9 153 370 371 407
Understand the operation
For 153 there are three digits, so calculate 1³ + 5³ + 3³ = 153. For 1634 there are four digits; fourth powers are required. A cube-only checker silently answers a narrower three-digit question.
Save the original value before repeatedly dividing a working copy by ten. % 10 obtains the last digit. A do-while processes zero as one digit when zero is included in the contract.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
static unsigned digits(unsigned value) {
unsigned count = 1;
while (value >= 10) { value /= 10; count++; }
return count;
}
static unsigned long long integer_power(unsigned base, unsigned exponent) {
unsigned long long result = 1;
for (unsigned i = 0; i < exponent; i++) result *= base;
return result;
}
static int is_armstrong(unsigned value) {
unsigned count = digits(value), remaining = value;
unsigned long long total = 0;
do { total += integer_power(remaining % 10, count); remaining /= 10; } while (remaining != 0);
return total == value;
}
int main(void) {
for (unsigned value = 1; value <= 1000; value++) if (is_armstrong(value)) printf("%u ", value);
putchar('\n');
return 0;
}
Expected output
1 2 3 4 5 6 7 8 9 153 370 371 407
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static unsigned digits(unsigned value) {
unsigned count = 1;
while (value >= 10) { value /= 10; count++; }
return count;
}
static unsigned long long integer_power(unsigned base, unsigned exponent) {
unsigned long long result = 1;
for (unsigned i = 0; i < exponent; i++) result *= base;
return result;
}
static int is_armstrong(unsigned value) {
unsigned count = digits(value), remaining = value;
unsigned long long total = 0;
do { total += integer_power(remaining % 10, count); remaining /= 10; } while (remaining != 0);
return total == value;
}
int main(void) {
for (unsigned value = 1; value <= 1000; value++) if (is_armstrong(value)) printf("%u ", value);
putchar('\n');
return 0;
}
The outer loop visits one through one thousand, including one-digit values.
Each digit contributes its integer power; the completed total is compared with the untouched original number.
Follow the worked calculation
Start with the sample
The outer loop visits one through one thousand, including one-digit values.
Apply the rule
Each digit contributes its integer power; the completed total is compared with the untouched original number.
Check the result
1 2 3 4 5 6 7 8 9 153 370 371 407
1 2 3 4 5 6 7 8 9 153 370 371 407
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static unsigned digits(unsigned value) {
unsigned count = 1;
while (value >= 10) { value /= 10; count++; }
return count;
}
static unsigned long long integer_power(unsigned base, unsigned exponent) {
unsigned long long result = 1;
for (unsigned i = 0; i < exponent; i++) result *= base;
return result;
}
static int is_armstrong(unsigned value) {
unsigned count = digits(value), remaining = value;
unsigned long long total = 0;
do { total += integer_power(remaining % 10, count); remaining /= 10; } while (remaining != 0);
return total == value;
}
int main(void) {
for (unsigned value = 1; value <= 1000; value++) if (is_armstrong(value)) printf("%u ", value);
putchar('\n');
return 0;
}
Use the changed interval shown in the worked answer and predict its matching values.
Worked answer
The worked answer uses the same general definition. It keeps digit count separate from the destructive digit-extraction loop, so an interval crossing a digit-length boundary still receives the correct exponent.
C17 · complete program
#include <stdio.h>
static unsigned digits(unsigned value) {
unsigned count = 1;
while (value >= 10) { value /= 10; count++; }
return count;
}
static unsigned long long integer_power(unsigned base, unsigned exponent) {
unsigned long long result = 1;
for (unsigned i = 0; i < exponent; i++) result *= base;
return result;
}
static int is_armstrong(unsigned value) {
unsigned count = digits(value), remaining = value;
unsigned long long total = 0;
do { total += integer_power(remaining % 10, count); remaining /= 10; } while (remaining != 0);
return total == value;
}
int main(void) {
for (unsigned value = 150; value <= 160; value++) if (is_armstrong(value)) printf("%u ", value);
putchar('\n');
return 0;
}
Expected output
153
Run in C compiler →Recognize the important boundaries
The fixture’s contract is nonnegative decimal values at most 999999, keeping digit-power sums representable. Larger numeric domains need checked multiplication and addition.
Avoid floating-point pow for exact small integer powers when a short integer multiplication loop can express the required exact arithmetic.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
An Armstrong number equals the sum of its decimal digits each raised to the number of decimal digits in the original number.
Continue with integer remainder and division.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.