Vadim Zaytsev aka @grammarware

BabyCobol: 100 Doors

We start with 100 closed doors and make 100 passes by them. On the first pass, we toggle the state of each door. On the second pass, we toggle the state of each second door. We continue, until on the hundredth pass we toggle the state of the last door. The solution can also be found on Rosetta Code.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ONE HUNDRED DOORS.
       DATA DIVISION.
       01 I PICTURE IS 9(3).
       01 J LIKE I.
       01 DOOR PICTURE IS 9 OCCURS 100 TIMES.
       01 STOP LIKE DOOR.
       PROCEDURE DIVISION.
      * Initialise the data
           MOVE HIGH-VALUES TO STOP
           MOVE SPACES TO DOOR.
      * Do the main algorithm
           LOOP VARYING I UNTIL DOOR(I) = 9
               LOOP VARYING J FROM I TO 100 BY I
                   SUBTRACT DOOR (J) FROM 1 GIVING DOOR (J)
               END
           END.
      * Print the results
           LOOP VARYING I UNTIL DOOR(I) = 9
               DISPLAY "Door" I "is" WITH NO ADVANCING
               IF DOOR (I) = 1
               THEN DISPLAY "open"
               ELSE DISPLAY "closed".
           END.

BabyCobol is a project by Dr. Vadim Zaytsev a.k.a. @grammarware. Page last updated in March 2024.
XHTML 1.1 CSS 3