Programming Examples

C Program to Display Armstrong Number Between Two Intervals


Write a C Program to Display Armstrong Number Between Two Intervals

Solution

#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 

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz