# Thread: factorials in recursive function

1. ## factorials in recursive function

Code:
```#include <iostream.h>
#include <stdlib.h>

void factorial(int);
int num;
int f;
void main ()
{

cout<<"enter number";
cin>>num;

factorial (5);

}
void factorial (int num)

{
if(num!=1)
{
cout<<"factorial is:"<<f<<endl;
exit(0)

else
{

f+f*num;
num--;

}

factorial (num)

}```
--------------------------------------------------------------------------

help me out with this, i keep getting it wrong

2. I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:
```for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}```
This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

Secondly, could you be more discriptive on what you're getting wrong? What is it doing? Is it compiling?

Good Luck,
Kermi3

3. f+f*num;
num--;

Perhaps you want to change that to:

f = f*num;
num--;

Otherwise, your results won't be stored.
Another thing. Perhaps you should try to restructure your factorial function. It is bad to be dependent on global variables, and you shouldn't exit the program from within it.
Code:
```int Factorial(int Num)
{
if(Num <= 1)
{
return 1;
}
else
{
return Factorial(Num - 1);
}
}```

4. ## Re: factorials in recursive function

Originally posted by CBeginner
Code:
```#include <iostream.h>
#include <stdlib.h>

void factorial(int);
int num;
int f;
void main ()
{

cout<<"enter number";
cin>>num;

factorial (5);

}
void factorial (int num)

{
if(num!=1)
{
cout<<"factorial is:"<<f<<endl;
exit(0)

else
{

f+f*num;
num--;

}

factorial (num)

}```
--------------------------------------------------------------------------

help me out with this, i keep getting it wrong