Armstrong Numbers between two integers?


An integer is called an Armstrong number of order n if it's every digit separate out and cubed and summed up then the sum will be same as the number i.e. abcd... = a3 + b3 + c3 + d3 + ...

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

153 = 13 + 53 + 33 // 153 is an Armstrong number.

Input: Enter two numbers(intervals):999 9999
Output: Armstrong numbers between 999 and 9999 are: 1634 8208 9474

Explanation

1634 = 13+63+33+43
= 1+216+27+64
= 1634

The approach implemented below is simple. We traverse through all numbers in the given range. For every number, we first count the number of digits in it. Let the number of digits in the current number be n. Them we find the sum of the cube of all digits. If the sum is equal to I, we print the number.

Example

#include <stdio.h>
#include <math.h>
int main() {
   int low = 100;
   int high = 400;
   printf("The amstrong numbers between %d and %d is \n",low,high);
   for (int i = low+1; i < high; ++i) {
      int x = i;
      int n = 0;
      while (x != 0) {
         x /= 10;
         ++n;
      }
      int pow_sum = 0;
      x = i;
      while (x != 0) {
         int digit = x % 10;
         pow_sum += pow(digit, n);
         x /= 10;
      }
      if (pow_sum == i)
         printf("%d ", i);
   }
   printf("\n");
   return 0;
}

Updated on: 19-Aug-2019

793 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements