Pattern Printing - Pyramid
Problem Description
Print a pyramid pattern of stars for N rows.
Expected Output
Pyramid pattern output
Hints
Each row has increasing stars with leading spaces.
Solution
IDENTIFICATION DIVISION.
PROGRAM-ID. PYRAMID.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-N PIC 9(2).
01 WS-ROW PIC 9(2).
01 WS-COL PIC 9(2).
01 WS-SPACES PIC 9(2).
01 WS-LINE PIC X(50).
01 WS-PTR PIC 9(2).
PROCEDURE DIVISION.
DISPLAY "ENTER NUMBER OF ROWS: ".
ACCEPT WS-N.
PERFORM VARYING WS-ROW FROM 1 BY 1 UNTIL WS-ROW > WS-N
MOVE SPACES TO WS-LINE
MOVE 1 TO WS-PTR
* Add leading spaces
COMPUTE WS-SPACES = WS-N - WS-ROW
PERFORM WS-SPACES TIMES
MOVE " " TO WS-LINE(WS-PTR:1)
ADD 1 TO WS-PTR
END-PERFORM
* Add stars (2*row - 1 stars)
COMPUTE WS-COL = (WS-ROW * 2) - 1
PERFORM WS-COL TIMES
MOVE "*" TO WS-LINE(WS-PTR:1)
ADD 1 TO WS-PTR
END-PERFORM
DISPLAY WS-LINE
END-PERFORM.
STOP RUN.
Explanation:
Row i has (n-i) spaces followed by (2*i-1) stars.