The factorial of n, written n!, is 1 times 2 times … up to n: 5! = 1 × 2 × 3 × 4 × 5 = 120. By rule, 0! = 1. Write factorial(n) to return n! (n is from 0 to 20).
factorial(5)→120factorial(3)→6factorial(1)→1long long factorial(int n)long long factorial(int n) {
long long f = 1;
for (int i = 1; i <= n; i++) {
// your code here: multiply f by i
}
return f;
}
Run tries the examples. When they look right, Submit.