Computer Science Canada

Failed Factorial

Author:  RyGuy-Anime [ Mon Mar 10, 2014 4:03 pm ]
Post subject:  Failed Factorial

So I haven't programmed in a while and am trying to get rid of that rust.... and then for some reason nothing seems to go properly. Anyways I'm writing a Factorial function and my variable keeps getting some insane value. It's a really quick fix but evidently I forgot what I'm doing let me know what I'm doing wrong.

#include <stdio.h>

int factorial (int n)
{
if (n <= 1)
return 1;

else
n = n*factorial(n-1);
return n;
}

int main()
{
int answer = factorial(4);
}[/list][/quote]

Author:  Insectoid [ Mon Mar 10, 2014 4:27 pm ]
Post subject:  RE:Failed Factorial

Works fine for me. Maybe you're printing the wrong thing?

Author:  RyGuy-Anime [ Mon Mar 10, 2014 4:41 pm ]
Post subject:  RE:Failed Factorial

#Maclife it's my first time using Xcode as an IDE so maybe it's a configuration issue?.... damn

Author:  Insectoid [ Mon Mar 10, 2014 5:49 pm ]
Post subject:  RE:Failed Factorial

I'm gonna take a stab in the dark and say you're doing this:

code:
printf ("%d\n", &answer)


This would output a really big number (a memory address), instead of the value of the variable. If this is the case, just remove the ampersand.

Author:  bl0ckeduser [ Tue Mar 11, 2014 8:58 pm ]
Post subject:  Re: Failed Factorial

Works perfect for me too, on Linux 64-bit.
Can you post your complete code including your print call ?

Author:  Msahayaza [ Tue Jan 23, 2018 3:54 am ]
Post subject:  RE:Failed Factorial

I want to know what this is really saying.


: