it may be by design but 100 factorial is gigantic and may not fit into int (i don't recall if it fits in unsigned 64 bit or not, but you can check it). Some professors will throw this out and let you get an incorrect value to debug, so fair warning.
if(n=t) says "assign n to the value of t and if the new n is not zero, proceed else skip"
you probably meant n==t which says if n and t are the same.
the 'simple and dumb way'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main()
{
uint64_t fact = 1;
double e = 1.0;
for(int i = 1; i < 30; i++)
{
fact *= i;
e += 1.0/fact;
}
cout <<setprecision(20) << e << endl;
}
output: 2.7182818284590450908
|
that is the answer until factorial gets too large. You can re-arrange it to avoid the factorials, which will eventually underflow a double but that is ok, you would just be adding zero a bunch of times doing nothing.
the formula they gave you can be done as well if you are not allowed to use the simple form (its the same equation, really, but its a bit to show that to be true!) You are trying to hard code the formula in your loop, and that won't work. You need to understand the formula and do one iteration worth each loop, building a running total. You need to unravel the MATH part of this before you try to CODE it. The code will be simple, I promise. The math may take you a few min fooling with it on paper to get to where you need to be to see what to do.