program to print all special numbers less than a given number (a number is special if it is equal to the sum of factorials of digit)

write a function for factorial and sum its digits and check for equality...it's simple..isn't it???

do u want a complete code??

i have tried it , bt prog is getting hanged

``````int x,i,n,d,sf=0,f,ncopy;
cin>>n;
for(n=1;n<x;n++)
ncopy=n;
{while(n>0)
{f=1;
d=n%10;
for(i=1;i<=d;i++)
{f=f*i;
}
sf=sf+f;
n=n/10;
}
if(ncopy==sf)
cout<<sf;
}
getch();}``````

use code tages for the code you post here

``````cin>>n;
for(n=1;n<x;n++)``````

what's that??? what is n?? you are taking value of n from user and in for loop you are overwriting it's value...what's x??and all other variables..better you make a separate function for calculating factorial...

``````int factorial(int n)
{
if(n==0)
return 1;
int fact=n;
while(--n)
fact*=n;
return fact;
}``````

now add it's digits and check for equality...

``````int sum(int c)
{
int r,sum=0;
do{
r=c%10;
c=c/10;
sum+=factorial(r);

}while(c);
return sum;
}``````

this function returns sum of the factorials of all the digits of a given numbers sent in c

``````cout<<"\nEnter number : ";
cin>>n;

for(int i=1 ; i<n ; i++)
{
if(i==sum(i))
cout<<i<<" ";
}``````

this will print all the numbers less than n which are special...

write a function for factorial and sum its digits and check for equality...it's simple..isn't it???

do u want a complete code??

Good point.

...
this function returns sum of the factorials of all the digits of a given numbers sent in c
...
this will print all the numbers less than n which are special...

What happened to your previous post? Did you forget?

commented: That's exactly what I thought :) +1

hehe i was expecting this...:icon_lol:
well he showed his program....so i just posted the code...
he showed his efforts atleast..so just did it..

hehe i was expecting this...:icon_lol:
well he showed his program....so i just posted the code...
he showed his efforts atleast..so just did it..

And rather than learning something useful, he learned he can post any thing and get the code to hand in from someone else... :icon_rolleyes: