PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to print all Armstrong number in a given range

Input - Output

Input
Enter two intervals : 20 500

Output
Armstrong number between 20 and 500 are : 153 3370 371 407

Algorithm

Step 1: START

Step 2: Declare variable low,high,i,rem,temp1,temp2,n=0

Step 3: Enter interval and store it into low and high.

Step 4: Check each number one by one.

step 6: End

CODE


/* Print all Armstrong number in a given range */

#include<stdio.h>
#include<math.h>
int main()
{
      int low,high,i,rem,temp1,temp2,n=0;
      float result = 0.0;
      printf(" Enter two intervals : ");
      scanf("%d %d", &low, &high);
      printf(" Armstrong numbers between %d and %d are : ",low,high);
      for(i=low+1;i<high;++i)
    {
        temp1 = i;
        while(temp1 != 0)
      {
          temp1 /= 10;
          ++n;
      }
        temp2 = i;
        while(temp2 != 0)
      {
          rem = temp2 % 10;
          result += pow(rem,n);
          temp2 /= 10;
      }
        if((int)result == i)
      {
         printf(" %d ",i);
      }
         n=0;
         result=0;
    }
    return 0;
}


/* Print all Armstrong number in a given range */

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
      int low,high,i,rem,temp1,temp2,n=0;
      float result = 0.0;
      cout<<" Enter two intervals : ";
      cin>>low>>high;
      cout<<" Armstrong numbers between "<<low<<" and "<<high<<" are : ";
      for(i=low+1;i<high;++i)
    {
        temp1 = i;
        while(temp1 != 0)
      {
          temp1 /= 10;
          ++n;
      }
        temp2 = i;
        while(temp2 != 0)
      {
          rem = temp2 % 10;
          result += pow(rem,n);
          temp2 /= 10;
      }
        if((int)result == i)
      {
         cout<<" "<<i;
      }
         n=0;
         result=0;
    }
    return 0;
}

OutPut

Enter two intervals : 20 500

Armstrong number between 20 and 500 are : 153 3370 371 407