5. A. GENERATE ALL AMSTRONG NUMBERS UPTO N.
DEFN: AN ARMSTRONG NUMBER OF THREE DIGITS IS AN
INTEGER SUCH THAT THE SUM OF THE CUBES OF ITS DIGITS
IS EQUAL TO THE NUMBER ITSELF.
EXAMPLE - 371 IS AN ARMSTRONG NUMBER, SINCE 3**3 + 7**3 +
1**3 = 371
SOURCE CODE:
#include <stdio.h>
void main(){
int r;
long n = 0, i, sum = 0, temp;
printf("Enter an integer upto which you want to find
armstrong numbers\n");
scanf("%ld",&n);
printf("Following armstrong numbers are found from 1 to
%ld\n",n);
for( i = 1 ; i <= n ; i++ )
{
temp = i;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( i == sum )
printf("%ld\n", i);
sum = 0;
}
}
0 comments:
Post a Comment