Wednesday, July 2, 2014

Strong Number

/* StrongNumber: Sum of factorial of digits in a number is equal to the number
i.e. 145 = 1 ! + 4 ! + 5 !
     145 = 1 + 24 + 125
     145 = 145
*/
#include<stdio.h>
int fact(int);
main()
{
int n,s,i;
for(i=0;i<200000;i++)
{
n=i;
s=0;
while(n)
{
s+=fact(n%10);
n/=10;
}
if(s==i)
printf("Strong Number:%d\n",i);
}
return 0;
}
int fact(int a)
{
if(a==0)
return 1;
else
return a*fact(a-1);
}

No comments:

Post a Comment