Easy+ · accumulator
Factorial
n! = 1 × 2 × … × n. The multiplication flavor of the accumulator — plus a hard lesson about how small an int really is.
Problem
Input: one integer n (0 ≤ n ≤ 12).
Output: n! (12! = 479,001,600 — the largest that fits in a 32-bit int).
Sample:
| Input | Output |
|---|---|
5 | 120 |
0 | 1 |
Your solution
Custom input for "Run my input"
Judging runs entirely in your browser against hidden tests. Your code is saved locally as you type. Ctrl+Enter judges.
Stuck? Hints
Hint 1 — multiply, starting from 1
long result = 1; then loop result = result * i;. Start at 1, not 0 — and 0! = 1 by definition, so the empty loop handles it.
Hint 2 — why n ≤ 12?
13! = 6,227,020,800 which is bigger than int.MaxValue (2,147,483,647) — it would silently wrap. Watch it happen: run Console.WriteLine(479001600 * 13); in the playground.
Solution (try first!)
int n = int.Parse(Console.ReadLine());
long result = 1;
for (int i = 2; i <= n; i++) result = result * i;
Console.WriteLine(result);
The overflow ceiling is the real lesson — see Lesson 2's trap #2. Next: Star Pyramid.