n factorial c++ code example
Example 1: c++ factorial
#include <cmath>
int fact(int n){
return std::tgamma(n + 1);
}
// for n = 5 -> 5 * 4 * 3 * 2 = 120
//tgamma performas factorial with n - 1 -> hence we use n + 1
Example 2: fcatorianls c++
#include <iostream>
//n! = n(n-1)!
int factorial (int n)
{
if (n ==0)
{
return 1;
}
else
{
return n * factorial(n-1);
}
}