Write a program to find the factorial of a given number in c++ code example
Example 1: program to calculate factorial of number in c++
using namespace std;
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
Example 2: c++ factorial
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