C Program to Print Armstrong Numbers in a Range

C Program to Print Armstrong Numbers in a Range

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.

  1. Explain

    Explain why armstrong interval uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use the changed interval shown in the worked answer and predict its matching values.

Armstrong Interval: follow the data

  1. 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.

  2. Rule

    An Armstrong number equals the sum of its decimal digits each raised to the number of decimal digits in the original number.

  3. Observed result

    153 370 371 407

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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 = 100; value <= 500; value++) if (is_armstrong(value)) printf("%u ", value);
  putchar('\n');
  return 0;
}

Expected output

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 = 100; value <= 500; value++) if (is_armstrong(value)) printf("%u ", value);
  putchar('\n');
  return 0;
}

The helper first counts the original decimal digits.

Each digit contributes its integer power; the completed total is compared with the untouched original number.

Follow the worked calculation

  1. Start with the sample

    The helper first counts the original decimal digits.

  2. Apply the rule

    Each digit contributes its integer power; the completed total is compared with the untouched original number.

  3. Check the result

    153 370 371 407

    153 370 371 407
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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 = 100; value <= 500; 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 = 1600; value <= 1700; value++) if (is_armstrong(value)) printf("%u ", value);
  putchar('\n');
  return 0;
}

Expected output

1634 
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.

Which exponent should the checker use for 1634?
Choose one answer. You can try again.

Answer and explanation

Four, because the original number has four decimal digits.

Correct. The definition follows the digit count rather than a fixed cube.

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.

    Next lesson: C Program to Reverse a Number →