I am working on code to calculate the factorial using below function:

int factorial (int num)
{
if (num==1)
return 1;
return factorial(num-1)*num; // recursive call
}


And I need to calculate the numbers from 1 to 255…

The problem occurs when it reaches number 35 and above, it starts to give -1.#IND000 as result for the calculation…

I tried to define the variable as float, double , long double but it doesn’t work!

Recommended Answers

All 3 Replies

I am working on code to calculate the factorial using below function:

int factorial (int num)
{
if (num==1)
return 1;
return factorial(num-1)*num; // recursive call
}


And I need to calculate the numbers from 1 to 255…

The problem occurs when it reaches number 35 and above, it starts to give -1.#IND000 as result for the calculation…

I tried to define the variable as float, double , long double but it doesn’t work!

An integer can only hold from -2^31 to 2^31 - 1 so I'm surprised you got even as high as 35! since that's way more than 2^31 - 1. Maybe that's because you tried those other data types, which can store larger numbers. What you need is arbitrary-precision data types which can go as high as you need, which is pretty high for 255!. Here's a link to a wikipedia article, which has some links to arbitrary-precision software.
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

first of all thanks guys for your replies...

the library http://gmplib.org

they said that it is the best in this regard but i couldn't know how to install it or use it in my C++ program :(

i tried to read the manual but no result...

thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.