Join our newsletter for the latest updates.

C Program to Display Armstrong Number Between Two Intervals

In this example, you will learn to find all Armstrong numbers between two integers entered by the user.

To understand this example, you should have the knowledge of the following C programming topics:


A positive integer is called an Armstrong number (of order n) if

abcd... = an + bn + cn + dn + 

In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because

153 = 1*1*1 + 5*5*5 + 3*3*3

In this program, we will print all the Armstrong numbers between two integers. This means that the two integers will not be part of the range, but only those integers that are between them.

For example, suppose we want to print all Armstrong numbers between 153 and 371. Both of these numbers are also Armstrong numbers.

Then, this program prints all Armstrong numbers that are greater than 153 and less than 371 i.e. 153 and 371 won't be printed even though they are Armstrong numbers.

Tip: Before trying this program, learn how to check whether an integer is an Armstrong number or not.


Armstrong Numbers Between Two Integers

#include <math.h>
#include <stdio.h>
int main() {
  int low, high, number, originalNumber, rem, count = 0;
  double result = 0.0;
  printf("Enter two numbers(intervals): ");
  scanf("%d %d", &low, &high);
  printf("Armstrong numbers between %d and %d are: ", low, high);

  // swap numbers if high < low
  if (high < low) {
    high += low;
    low = high - low;
    high -= low;
  }
   
  // iterate number from (low + 1) to (high - 1)
  // In each iteration, check if number is Armstrong
  for (number = low + 1; number < high; ++number) {
    originalNumber = number;

    // number of digits calculation
    while (originalNumber != 0) {
      originalNumber /= 10;
      ++count;
    }

    originalNumber = number;

    // result contains sum of nth power of individual digits
    while (originalNumber != 0) {
      rem = originalNumber % 10;
      result += pow(rem, count);
      originalNumber /= 10;
    }

    // check if number is equal to the sum of nth power of individual digits
    if ((int)result == number) {
      printf("%d ", number);
    }

    // resetting the values
    count = 0;
    result = 0;
  }

  return 0;
}

Output

Enter two numbers(intervals): 200
2000
Armstrong numbers between 200 and 2000 are: 370 371 407 1634 

In the program, the outer loop is iterated from (low+ 1) to (high - 1). In each iteration, it's checked whether number is an Armstrong number or not.

Inside the outer loop, the number of digits of an integer is calculated first and stored in count. And, the sum of the power of individual digits is stored in the result variable.

If number is equal to result, the number is an Armstrong number.

Notes:

  • You need to swap low and high if the user input for high is less than that of low. To learn more, check our example on swapping two numbers.
  • You need to reset count and result to 0 in each iteration of the outer loop.
Did you find this article helpful?