Kodewick
Empieza a aprender →
Básico←→0 / 50

Factorial recursivo

#046Básicon * fact(n - 1)4 min

El factorial de n es n! = 1 × 2 × … × n, y por definición 0! = 1. Esta vez sin bucles: escribe fact(n) con recursión. n! es n × (n-1)!; por ejemplo, 5! = 5 × 4! = 120. El código inicial ya tiene «si n es 0 o 1, devuelve 1»; tú escribes la línea que falta.

Ejemplos
fact(5)→120
fact(0)→1
fact(3)→6
La funciónlong long fact(int n)
main.c
long long fact(int n) {
    if (n <= 1) {
        return 1;
    }
    // tu código aquí: devuelve n por el factorial de (n-1)
    return 0;
}
Ctrl + Enter también ejecuta
Resultado

Ejecutar prueba los ejemplos. Cuando salgan bien, Enviar.