Thread: Recursive function outputs the wrong value at higher numbers?!

  1. #1
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011
    Location
    Hell.
    Posts
    14

    Recursive function outputs the wrong value at higher numbers?!

    OK so I made this recursive function for getting the factorial of a number. Problem is, It only prints out the right answer for inputs up to 12. I can't get it to output the right answer for inputs that are higher than 12.

    Code:
    int factorial(int input
    {
    
    	if(input==1){
    	
    	return 1;
    	}
    	else return input*(factorial(input-1));
    	
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    integer has limited size(usually 4 bytes). It cannot hold all values...

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    integer has limited size(usually 4 bytes). It cannot hold all values...
    Yep... just checked it... an input of 13 overflows a 32 bit integer.

  4. #4
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011
    Location
    Hell.
    Posts
    14
    Thanks.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheWhiffet View Post
    Thanks.
    So... you're not at all curious how to check this for yourself?

  6. #6
    Registered User Alexander.'s Avatar
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    There are various solutions to implement to extend the function so that it can utilize higher numbers,

    how do you think your issue can be fixed in your own words? How do you think the operations can happen in a manner is not held by the bound of a single integer?

    Curiosity is an amazing learning tool for programming, that it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  2. int vector outputs wrong numbers
    By Vandrian in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 12:15 PM
  3. Having only one function run at higher priority
    By ulillillia in forum C Programming
    Replies: 35
    Last Post: 02-24-2008, 06:56 PM
  4. Replies: 8
    Last Post: 09-11-2006, 11:26 AM
  5. Finding numbers higher than the first element in array
    By sonict in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 10:00 AM