Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Sunday, January 9, 2011

Write a program to print all Armstrong numbers between 1 & 500. If the sum of cubes of each digit of the no. is equal to the no. itself, then the no. is called an Armstrong Number.


Source Code :

#include<stdio.h>
void main()
{
      int b,c=0,div,rem,i,j,n,ctr=0,temp[5];
      clrscr();
      printf("\nArmstrong Numbers between 1 to 500 : ");
      for(n=1;n<=500;n++)
      {
            div=n;
            ctr=0;
            while(div!=0)
            {
                  rem=div%10;
                  temp[ctr]=rem;
                  div=div/10;
                  ctr++;
            }
            c=0;
            for(i=0;i<ctr;i++)
            {
                  b=1;
                  for(j=0;j<ctr;j++)
                  {
                        b=b*temp[i];
                  }
                  c=c+b;
            }
            if(c==n)
                  printf("%d, ",n);
      }
      getch();
}

Output :

Armstrong Number between 1 to 500 : 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,

No comments:

Post a Comment