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)

4
Contributors
12
Replies
13
Views
10 Years
Discussion Span
Last Post by rajatC
Featured Replies
• 1
WaltP 2,905   10 Years Ago

[QUOTE=rajatC;500081]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?? if yes then sorry..nobody will help you here for that...[/QUOTE] Good point. [QUOTE=rajatC;500114] ... this function returns [B]sum of the factorials of all the digits[/B] of a given numbers …

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?

That's exactly what I thought :)

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:

Very true. Helping them write the code is a greater help to them in the long term.

oopsssss!!! sorry
my intention was only to help....i will take care in future..

oopsssss!!! sorry
my intention was only to help....i will take care in future..

I understand your goodwill, but it would appear to anyone who does not know you that you were doing his homework. Please reconsider your actions in future :).

got it

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.