explain the result fact (100) = 0!!

I tried to calculate the factorial of a number n equal to 100 and the result gives me 0. explain the result fact (100) = 0 please :)
Can't explain if you have not shown code. For all I know your code could be returning 0 for any number N.
My guess is that your loop is including 0 so that it multiplies with zero and therefore the result becomes zero.
Last edited on
thank you friends, this is my code:

#include "/home/src/prog.hpp"
unsigned long fact(unsigned short n) {
return n<=1 ? 1 : n*fact(n-1);
}
unsigned short n;
int main() { cout<<"\nn="; cin>>n;
cout<< n <<"! = "<< fact(n)<< endl;
return 0;
}
Hi,

fact(100) is rather a large number - bigger than what a double can handle, and certainly bigger than an unsigned 64 bit quantity, so you need an extended number library like gmp (there are others) - have a look at boost library.

Please always use code tags - use the <> button on the format menu, when editing your topic.

And can we ask you not to do duplicate posts, just keep the same one going. If you don't get replies, then reply to your own topic, and it will turn up at the top of the list again. If you think it is on the wrong board, then you can edit your post, and move it to a different one.

Cheers :+)
100! is a very big number, chances are your architecture's "unsigned long" data type is not big enough to hold it. In fact, I doubt any built-in types will be big enough.

I would search for a "BigNumber" C++ library on google, or come up with your own custom arithmetic that works on a large array or dynamic string.
Topic archived. No new replies allowed.