Saturday, January 30, 2010

CHAP 3[B]e Printing armstrong numbers

Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

void main()
{
int a,b,c,d;
d=2;
printf("\nThe program will print out Armstrong numbers between 1 to 500");

while(d<500)
{
a=d/100;
b=(d/10)-(a*10);
c=(d/1)-(a*100)-(b*10);

a=a*a*a;
b=b*b*b;
c=c*c*c;

if(a+b+c==d)
printf("\n%d\n",d);

d++;
}
}

No comments:

Post a Comment