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)

Recommended Answers

All 12 Replies

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...

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??
if yes then sorry..nobody will help you here for that...

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:

Please help them, we know you can write it. They're the ones that need to learn.

Please help them, we know you can write it. They're the ones that need to learn.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.