hello i am just starting in cpp

i am trying to find factorial of 99

i used unsigned long to store it but it shows the result as zero can any one tell me what sort of data type i should use to get the proper result

thanz in advance

Printable View

- 04-09-2006saminenivinaystore vaue which is more than 25 digits
hello i am just starting in cpp

i am trying to find factorial of 99

i used unsigned long to store it but it shows the result as zero can any one tell me what sort of data type i should use to get the proper result

thanz in advance - 04-09-2006LinuxCoder
Try using floats or doubles. They behave quite differently from ints since they are floating point datatypes but i think it's the easiest way to accomplish what you are trying to

- 04-09-2006laserlight
You could try using the GNU Multiple Precision Arithmetic Library.

- 04-09-2006jafet
99! has 156 digits:

Code:`933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000`

It comes out as zero because unsigned long on a 32-bit machine is 4294967296 and 99! % 4294967296 == 0

Code:`99! == 4294967296 (ie 2 ^ 32) *`

217292028115932253845267088283474325413620025535071318663174668083008378728362535147325750384844183656977717580337132695715840000000000000000000000

Even on a 64-bit machine:

Code:`99! == 18446744073709551616 (ie 2 ^ 64) *`

50592242767087242995683822846849059083876205965660353811265590619996278250674376065685821321058129811887097912919040000000000000000000000

Option #1: Use double, but forego accuracy.

Option #2: Use a bignum library like GMP or Apfloat.

Option #3: Accept the fact that n! for n > 33 will always return 0 ;)