Kodewick
Start learning →
Basic←→0 / 50

Recursive factorial

#046Basicn * fact(n - 1)4 min

The factorial n! = 1 × 2 × … × n, and 0! = 1. This time, no loop: use recursion to write fact(n). n! is n × (n-1)!, so 5! = 5 × 4! = 120. The starter already returns 1 when n is 0 or 1; you write the last line.

Examples
fact(5)→120
fact(0)→1
fact(3)→6
The functionlong long fact(int n)
main.c
long long fact(int n) {
    if (n <= 1) {
        return 1;
    }
    // your code here: return n times the factorial of (n-1)
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.