 PROCEDURE DIVISION.
 S100-MAIN SECTION.
 0100-MAIN.
     PERFORM S200-CALCULATOR-CONTROL
         TEST AFTER
         UNTIL USER-IS-DONE.
     STOP RUN.
 S200-CALCULATOR-CONTROL SECTION.
 0200-CALCULATOR-CONTROL.
     DISPLAY ‘ENTER 18-DIGIT NUMBERS SEPARATED BY OPERATION AS’.
     DISPLAY ‘123456789012345678 X 123456789012345678’.
     ACCEPT COMPUTE-INFO.
     MOVE ZERO TO RESULT.
     MOVE ‘Y’ TO DATA-VALID-INDICATOR.
     PERFORM S300-EDIT-INPUT.
     IF VALID-DATA
         PERFORM S400-CALCULATE
         MOVE RESULT TO SHOW-RESULT
         DISPLAY ‘ THE ANSWER IS: ‘ SHOW-RESULT.
     DISPLAY ‘ARE YOU FINISHED WITH YOUR CALCULATIONS? (Y/N): ‘.
     ACCEPT LOOP-CONTROL.

 S300-EDIT-INPUT SECTION.
 0300-EDIT-INPUT.
     IF FIELD-1 NOT NUMERIC
        DISPLAY ‘FIRST FIELD NOT A VALID NUMBER’
        MOVE ‘N’ TO DATA-VALID-INDICATOR.
     IF FIELD-2 NOT NUMERIC
        DISPLAY ‘SECOND FIELD NOT A VALID NUMBER’
        MOVE ‘N’ TO DATA-VALID-INDICATOR.
     IF NOT VALID-OPERATION
        DISPLAY ‘UNKNOWN CALCULATION’
        MOVE ‘N’ TO DATA-VALID-INDICATOR.
     IF NOT VALID-DATA
        DISPLAY ‘  RE-ENTER IF YOU WISH’
        GO TO 0300-EXIT.
     IF OPERATION = ‘/’ AND FIELD-2 = ZERO
        DISPLAY ‘DIVIDE BY ZERO NOT PERMITTED, RE-ENTER IF YOU WISH’
        MOVE ‘N’ TO DATA-VALID-INDICATOR.
 0300-EXIT.
     EXIT.

 S400-CALCULATE SECTION.
 0400-CALCULATE.
     IF OPERATION = ‘+’
        ADD FIELD-1 FIELD-2 GIVING RESULT
        GO TO 0400-EXIT.
     IF OPERATION = ‘-‘
        SUBTRACT FIELD-2 FROM FIELD-1 GIVING RESULT
        GO TO 0400-EXIT.
     IF OPERATION = ‘*’
        MULTIPLY FIELD-1 BY FIELD-2 GIVING RESULT
          ON SIZE ERROR
            DISPLAY ‘MULTIPLY OVERFLOW, SIGNIFICANT DIGITS LOST’
            MULTIPLY FIELD-1 BY FIELD-2 GIVING RESULT
            END-MULTIPLY
        END-MULTIPLY
        GO TO 0400-EXIT.
     IF OPERATION = ‘/’
        DIVIDE FIELD-1 BY FIELD-2 GIVING RESULT ROUNDED
          ON SIZE ERROR
            DISPLAY ’DIVIDE OVERFLOW, UNSPECIFIED RESULTS’
        END-DIVIDE
        GO TO 0400-EXIT.
     IF OPERATION = ‘^’
        MOVE 1 TO RESULT
        MOVE ‘N’ TO OVERFLOW-CONTROL
        PERFORM
            VARYING POWER-VALUE FROM 1 BY 1
            UNTIL POWER-VALUE > FIELD-2
              MULTIPLY RESULT BY FIELD-1 GIVING RESULT
               ON SIZE ERROR
                 DISPLAY ‘OVERFLOW AT POWER = ‘ POWER-VALUE
                 SUBTRACT 1 FROM POWER-VALUE
                 DISPLAY ‘VALUE AT ‘ POWER-VALUE ‘ WAS: ‘
                 MOVE FIELD-2 TO POWER-VALUE
              END-MULTIPLY
        END-PERFORM
     END-IF.
 0400-EXIT.
     EXIT.
