C/Sum of Cubes of numbers
Expert: Prince M. Premnath - 8/31/2009
QuestionWrite a program that examines all the numbers from 1 to 999, displaying all those for which the sum of cubes of the digits equals the number itself. For example, 153, 371.
1^3+5^3+3^3=153
3^3+7^3+1^3=371 ans so on....
**************
Sir,
I am not getting the solution of this question, please make the correction so that I get the answer. Given below the wrong solution :
#include<stdio.h>
main()
{
int i,rem,z;
long sum=0;
printf("\nEnter the mximum number :");
scanf("%d",&i);
while(i>0)
{
rem=i%10;
sum=sum*10+rem;
i=i/10;
z=sum*sum*sum;
printf("Answer: %d",z);
}
getch();
}
AnswerHi dear Abhimanyu !
Sorry for the delay , since i was busy with some project issues , kindly check out the following code!
#include<stdio.h>
void main()
{
int i,n,temp,digit;
long sum=0;
printf("\nEnter the mximum number :");
scanf("%d",&n);
for( i = 0; i <=n ;i++)
{
digit =0;
sum=0;
temp = i;
while(temp >0)
{
digit = temp%10;
sum+=digit * digit *digit;
temp = temp/10;
}
if( sum == i)
{
printf("%d\n" , sum);
}
}
getch();
}
Note: I didn't test this code , kindly revert me in case of issues !
Thanks and Regards!
Prince M. Premnath