IDENTIFICATION DIVISION.

PROGRAM-ID. FACTORIAL RECURSIVE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.

DATA DIVISION.

WORKING-STORAGE SECTION.
01 NUMB PIC 9(4) VALUE 5.
01 FACT PIC 9(8) VALUE 0.

LOCAL-STORAGE SECTION.
01 NUM PIC 9(4).

PROCEDURE DIVISION.
    MOVE NUMB TO NUM.
    IF NUMB = 0
        MOVE 1 TO FACT
    ELSE
        SUBTRACT 1 FROM NUMB
        CALL "FACTORIAL"
        MULTIPLY NUM BY FACT
    END-IF.
    DISPLAY NUM "! = " FACT.
    GOBACK.
END PROGRAM FACTORIAL.

IDENTIFICATION DIVISION.

PROGRAM-ID. FACTORIAL ITERATION.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
DATA DIVISION.

LOCAL-STORAGE SECTION.
01  i      PIC 9(10).

LINKAGE SECTION.
01  n      PIC 9(10).
01  ret    PIC 9(10).

PROCEDURE DIVISION USING BY VALUE n RETURNING ret.
    MOVE 1 TO ret

    PERFORM VARYING i FROM 2 BY 1 UNTIL n < i
        MULTIPLY i BY ret
    END-PERFORM

    GOBACK
END PROGRAM FACTORIAL.
