Possible Duplicates:
Calculating large factorials in C++
Howto compute the factorial of x
How do you implement the factorial function in C++? And by this I mean properly implement it using whatever argument checking and error handling logic is appropriate for a general purpose math library in C++.
Recursive:
Iterative:
Compile time:
You might want to take a look at
boost/math/special_functions/factorials.hpp
if you have Boost installed. You can read about it at: Boost FactorialBesides the obvious loops and recursions, modern C++ compilers support the gamma function as
tgamma()
, closely related to factorial:test run: https://ideone.com/TiUQ3