Saturday, 27 July 2013

Chapter 3 solutions Problem [B][e]

Question-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 )
Solution-
Good question and with some new information.i hadn't heard about Armstrong numbers. well, thanks to the book. let's look the solution.

#include<stdio.h>
int main()
{
int a,b,c,i,k;
for(i=2;i<=500;i++) /* i excluded 0,1 otherwise they would have been included */
{
k=i;
a=k%10;
k=k/10;
b=k%10;
k=k/10;
c=k%10;
if((a*a*a)+(b*b*b)+(c*c*c)==i)
printf("%d ",i);
}
return 0;
}

No comments:

Post a Comment