basics · beginner · ~15 min

Iterative factorial

Accumulate a product in a loop.

Challenge

Implement long factorial_iter(int n) returning n! using a loop. factorial_iter(0) == 1; assume n >= 0.

Starter code

long factorial_iter(int n) {
    /* TODO */
    return 1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.