C Program to Display Armstrong Number Between Two Intervals

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

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);

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

Note: You need to reset count and result to 0 in each iteration of the outer loop.