Hello,

So my program (to calculate n factorial) works well, except for one thing, it won’t output correctly after 12. My other 2 projects working with factorials ( calculating the constant e and e^x) worked because I used data type long. I tried doing it with my program but it did not work, I am not sure I was doing something wrong.
This post has a solution which I ran and it has the same issue:

``````#include "Factorial.h"		// include definition of class Factorial

// declaration and input phase

// validate the input
void Factorial::validate(int enteredInteger)
{

}
// constructor
Factorial::Factorial(int enteredInteger)
{
setVariables(enteredInteger);
}
// set functions to initialize the variables
void Factorial::setVariables(int enteredInteger)
{
nFactorial = 1;
value = enteredInteger;
factor = enteredInteger;
}
void Factorial::setN(int enteredInteger)
{
n = enteredInteger;
}
int Factorial::getFactorial()
{
return nFactorial;
}
int Factorial::getInteger()
{
return n;
}
void Factorial::compute()
{
int integer;

cout << "Enter a nonnegative integer to compute its factorial: ";
cin >> integer;
validate(integer);
setN(integer);

do
{
factor = n - value;
nFactorial *= factor;
value += 1;
} while (value < n);

displayMessage();
}
void Factorial::displayMessage()
{
cout << "The n factorial of " << getInteger() << "!" << " is " << getFactorial() << "." << endl;
}``````
3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Salem

I don't see where you declare member nFactorial, but you've pretty much answered your own question. Data type int or even long will not hold a value beyond 12!. You need to either use type double (good to about 21! IIRC), or, if your system supports it, a long long (64bits)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.