July 25, 2014

Factorial of a number in C & C++ | The Simplest Way

13:19 Posted by Durga swaroop Perla 1 comment

Factorial of a number is a very commonly used function and so, not surprisingly programmers run in to this quite often. But, there is no direct function we can call to do this for us. So, each time i have to use this, i end up writing a 5-10 line code using recursion or some other method and then call that for the factorial, which is pretty boring.

But, I have found a very easy way to implement factorial with a built-in function of "math.h" called tgamma(). So, lets see how to implement this program
#include <math.h>
#include <stdio.h>

double fact(double x)
{
  return tgamma(x+1);
}
int main()
{