# My factorial program keeps returning wrong values

Page 1 of 1

## 4 Replies - 906 Views - Last Post: 15 January 2013 - 02:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307414&amp;s=bebbc2ddc22ecbbf88283b2e879437f4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jbf1001

Reputation: 1
• Posts: 31
• Joined: 24-October 12

# My factorial program keeps returning wrong values

Posted 15 January 2013 - 01:40 PM

here is my code:
```#include <iostream>
using namespace std;

long fact(int x);

int main()
{
int num1;

cout << "Hello, please enter a whole number(1, 2, 3...) to find its factorial." << endl;
cout << "Please do not use decimals." << endl;

cout << ":: ";
cin >> num1;

cout << "The number " << num1 << "'s facorial is: ";
cout << fact(num1) << endl;
}

long fact(int x)
{

for(int num = x; num > 1; num--)
{
x = x * (num -1);
}

return x;
}

```

So here is the issue. If any number below twelve is used, then the program works as expected. Any value above 12 gives me either a large number that is not a correct factorial or it gives me a number that is not correct and has a - sign at the front. I think it much have something to do with the variable type(as you can see I tried to have the function return a 'long' value but I don't think I did that right), but maybe my code is wrong in other places?

Is This A Good Question/Topic? 0

## Replies To: My factorial program keeps returning wrong values

### #2 jimblumberg

Reputation: 5421
• Posts: 16,900
• Joined: 25-December 09

## Re: My factorial program keeps returning wrong values

Posted 15 January 2013 - 01:51 PM

For a factorial of 13 what is the factorial?

What compiler are you using?

What is the maximum value an "int" can contain using your compiler and operating system?

Jim

This post has been edited by jimblumberg: 15 January 2013 - 02:01 PM
Reason for edit:: changed "long" to "int"

### #3 Jbf1001

Reputation: 1
• Posts: 31
• Joined: 24-October 12

## Re: My factorial program keeps returning wrong values

Posted 15 January 2013 - 02:01 PM

I am using Visual Studio
The max is 2147483647
and the true factorial is 6227020800 but what it gives me is 1932053504

This post has been edited by Jbf1001: 15 January 2013 - 02:02 PM

### #4 jimblumberg

Reputation: 5421
• Posts: 16,900
• Joined: 25-December 09

## Re: My factorial program keeps returning wrong values

Posted 15 January 2013 - 02:06 PM

Actually since you are using an int in your function:
```long fact(int x)
{

for(int num = x; num > 1; num--)
{
x = x * (num -1); // x is an int!
}

return x;  // x is an int!
}

```

Your problem is compounded. What you are seeing it the effects of integer overflow. And since you are using a signed int, this leads to undefined behaviour.

Jim

This post has been edited by jimblumberg: 15 January 2013 - 02:07 PM

### #5 Jbf1001

Reputation: 1
• Posts: 31
• Joined: 24-October 12

## Re: My factorial program keeps returning wrong values

Posted 15 January 2013 - 02:11 PM

Ahhh thank you! I understand now. This is one of those 'too close to the problem to see the solution' kind of days haha.