1
Factorial
Problem Solving
Formula: n! = n × (n-1) × ... × 1, and 0! = 1.
Example: 5! = 120
int n = 5;
long long fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
cout << fact; // 120
long long fact(int n){
if(n <= 1) return 1;
return n * fact(n - 1);
}
Note: 20! is the largest factorial that fits in long long.