PERFORM TIMES
Problem Description
Execute a paragraph a specific number of times.
Expected Output
Loop with fixed iterations
Hints
PERFORM para-name N TIMES.
Solution
IDENTIFICATION DIVISION.
PROGRAM-ID. TIMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNTER PIC 9(2) VALUE 0.
01 WS-N PIC 9(2).
01 WS-FACTORIAL PIC 9(10) VALUE 1.
01 WS-I PIC 9(2) VALUE 1.
PROCEDURE DIVISION.
* Simple TIMES loop
DISPLAY "COUNTING TO 5:".
PERFORM DISPLAY-COUNT 5 TIMES.
* Calculate factorial
DISPLAY "ENTER N FOR FACTORIAL: ".
ACCEPT WS-N.
MOVE 1 TO WS-FACTORIAL.
MOVE 1 TO WS-I.
PERFORM CALC-FACTORIAL WS-N TIMES.
DISPLAY WS-N "! = " WS-FACTORIAL.
STOP RUN.
DISPLAY-COUNT.
ADD 1 TO WS-COUNTER.
DISPLAY "COUNT: " WS-COUNTER.
CALC-FACTORIAL.
MULTIPLY WS-I BY WS-FACTORIAL.
ADD 1 TO WS-I.
Explanation:
PERFORM para-name N TIMES executes paragraph N times.