# Problem with Recursion calculing Factorials

• 12-12-2005
cvelasquez
Problem with Recursion calculing Factorials
Well, I've been learning those things that I didn't knew in theC tutorial of this site, and, in the recursion chapter, there was a homework where you had to write a program which calculates factorials.

This is my function:
Code:

```int ObtenerFactorial( int num, int count, int result ) {         if ( count==num ) return result;         else                 {                         result *= (num-count);                         ObtenerFactorial( num, count+1, result );                 } }```
I call the function from main this way:
Code:

```fflush(stdin); scanf( "%d", &number ); factorial = ObtenerFactorial( (int)number, 0, 1 ); printf( "\nFactorial de %d: %d\n", number, factorial);```
Where 'number' is entered throw the command line.

When the user enter 0, the result is fine, 1. But when you enter any number greater than 0 (1,5,etc), everytimes get out the same result: 1075144832. I don't know what is the problem... I don't expect that you write my homework, just to tell what's wrong: if it's all wrong, I'll write a new program.
• 12-12-2005
Rashakil Fol
You meant return ObtenerFactorial( num, count+1, result );
• 12-12-2005
quzah
Quote:

I call the function from main this way:
You started out with the first line of your code being wrong. That's usually a bad way to start.

Quzah.
• 12-12-2005
cvelasquez
Thanks for your help, but I don't understand:

FIRST: no, I meant return result;, because if the number is equal to zero, it returns one, and if the count is equal to, for example, 5, in a factorial you don't have "5!=0*1*2*3*4*5", there is no a zero.

SECOND: I really don't understand. That is a part of the main function, if you are refering to the "#include... int main...", if it's not that, I don't know what could be.
• 12-12-2005
Dave_Sinkula
FAQ > Explanations of... > Why fflush(stdin) is wrong

FAQ > How do I... (Level 1) > How do I get a number from the user (C)
• 12-13-2005
Silfer
Err.. wait. I am not 100% sure here, for my memory of recursion is rusty. However, only your outermost call will return. It returns to the call that called it. You don't save that value, and it is lost. So first poster is right, your line
Code:

`ObtenerFactorial( num, count+1, result );`
should be
Code:

` return ObtenerFactorial( num, count+1, result );`
That way, first call returns the results of the second call who returns the results of the third call etc., and you get the results in your variable. Now, you get no results in it, and thus it's undefined. Try setting factorial to 0, you'll get 0 as answer.

edit: As an addendum, the other posters are also right, so don't flush stdin and such.
• 12-13-2005
ssharish2005
Code:

`factorial = ObtenerFactorial( (int)number, 0, 1 );`
you dont need to type cast it in this. its not need.#

ssharish2005
• 12-14-2005
cvelasquez
yeah, later I could see that it's not needed a cast because scanf( "%d", &number) receive only decimals.
I change that line 'ObtenerFact...' for 'return ObtenerFact...', and it works now, thanks you.