typestar

DIVIDE with REMAINDER in COBOL

One DIVIDE fills the boxes and the basket: quotient and remainder in a single verb.

IDENTIFICATION DIVISION.
PROGRAM-ID. DIVIDE-REMAINDER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EGG-COUNT   PIC 9(3) VALUE 157.
01 BOX-SIZE    PIC 99   VALUE 12.
01 FULL-BOXES  PIC 99.
01 LEFT-OVER   PIC 99.
PROCEDURE DIVISION.
    DIVIDE EGG-COUNT BY BOX-SIZE
        GIVING FULL-BOXES REMAINDER LEFT-OVER
        ON SIZE ERROR DISPLAY "box math overflowed"
    END-DIVIDE
    DISPLAY EGG-COUNT " eggs fill " FULL-BOXES " boxes"
    DISPLAY LEFT-OVER " stay in the basket"
    STOP RUN.

How it works

  1. DIVIDE ... GIVING FULL-BOXES REMAINDER LEFT-OVER names both results.
  2. ON SIZE ERROR guards the arithmetic; END-DIVIDE closes the scope.
  3. 157 eggs by the dozen: 13 boxes and one egg still in the basket.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
453
Tokens
61
Three-star pace
40 tpm

At the three-star pace of 40 tokens a minute, this run takes about 92 seconds.

Type this snippet

Step 3 of 3 in Arithmetic, step 12 of 27 in Language basics.

← Previous Next →