4 Replies - 500 Views - Last Post: 15 January 2013 - 02:11 PM Rate Topic: -----

#1 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • 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"

Was This Post Helpful? 0
  • +
  • -

#3 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1