Monday, 7 July 2014

Finding Armstrong Number Using loops



I have a new program for finding Armstrong Numbers.Last program that i wrote was not properly generalized for all the numbers.Here is the new one,which can be used to find Armstrong Numbers for a longer range.


#include<stdio.h>
int main()
{
   int i=1,g,a,num;
    while(i<500)
      {
        g=i;
        num=0;
    while(g>0)
      {
        a=g%10;
        num=num+a*a*a ;
        g/=10;
      }
    if(num==i)
        printf("%d\n",num);
        i++;

      }

  return 0;

}


This method is very efficient for longer rangers (i<=10000) , but since Armstrong Numbers can't be too big. it's doesn't help in finding more Armstrong numbers.There is caution ,which is supposed to be taken is that when we initialize num , it should be done before the second loop . initializing num before first loop or in the variables won't work and it will give you unexpected results.

No comments:

Post a Comment