Drill

ProblemsC# › warmup

Factorial

easywarmupMathRecursionC#

Return n! — the product of every integer from 1 to n.

Factorial(n: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int Factorial(int n) {
    
}

Worked examples

CallResult
Factorial(0)1
Factorial(1)1
Factorial(5)120
Factorial(10)3628800

Hint

Start an accumulator at 1 and multiply up to n. A loop is plenty.

Reference solution in C#
public int Factorial(int n) {
    int acc = 1;
    for (int i = 2; i <= n; i++) acc *= i;
    return acc;
}

The same problem in another language

More warmup problems in C#