← All examples

Factorial (loop)

C++ Loops

for with accumulation

Flowchart (ISO 5807)

StartInput nr = 1i = 2, n, 1r *= iReturn rEndFigure 1 — fact

Source code

long fact(int n) {
    long r = 1;
    for (int i = 2; i <= n; i++) {
        r *= i;
    }
    return r;
}