Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Failed Factorial
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RyGuy-Anime




PostPosted: 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]
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Mar 10, 2014 4:27 pm   Post subject: RE:Failed Factorial

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




PostPosted: 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
Insectoid




PostPosted: 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.
bl0ckeduser




PostPosted: 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 ?
Msahayaza




PostPosted: Tue Jan 23, 2018 3:54 am   Post subject: RE:Failed Factorial

I want to know what this is really saying.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: