Monday, 1 October 2012

Program to generate and print Armstrong Numbers using C


#include<stdio.h>
#include<conio.h>
 
main()
{
   int r;
   long numb = 0, c, sum = 0, temp;
 
   printf("Enter the maximum range upto which you want to find armstrong numbers ");
   scanf("%ld",&numb);
 
   printf("Following armstrong numbers are found from 1 to %ld\n",numb);
 
   for( c = 1 ; c <= numb ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
      if ( c == sum )
         printf("%ld\n", c);
      sum = 0;
   }
 
   getch();
   return 0;
}

Output:
Enter the maximum range up to which you want to find armstrong    numbers 1000
Following armstrong numbers are found from 1 to 1000
1
153
370
371
407


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...