Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL DIVIDE Statement

Beginner 🕑 10 min read 👁 0 views

COBOL DIVIDE Statement

The DIVIDE statement divides one numeric value by another.

Syntax

DIVIDE identifier-1 INTO identifier-2
DIVIDE identifier-1 INTO identifier-2 GIVING identifier-3
DIVIDE identifier-1 BY identifier-2 GIVING identifier-3
DIVIDE ... GIVING ... REMAINDER identifier

Example Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. DIV-DEMO.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-TOTAL       PIC 9(5) VALUE 10000.
       01 WS-DIVISOR     PIC 9(2) VALUE 3.
       01 WS-QUOTIENT    PIC 9(5) VALUE 0.
       01 WS-REMAINDER   PIC 9(2) VALUE 0.

       PROCEDURE DIVISION.
      * DIVIDE INTO
           DIVIDE 4 INTO WS-TOTAL
           DISPLAY 'After DIVIDE INTO: ' WS-TOTAL

      * DIVIDE BY GIVING
           MOVE 10000 TO WS-TOTAL
           DIVIDE WS-TOTAL BY WS-DIVISOR
               GIVING WS-QUOTIENT ROUNDED
           DISPLAY 'Quotient: ' WS-QUOTIENT

      * DIVIDE with REMAINDER
           DIVIDE WS-TOTAL BY WS-DIVISOR
               GIVING WS-QUOTIENT
               REMAINDER WS-REMAINDER
           DISPLAY 'Quotient: ' WS-QUOTIENT
           DISPLAY 'Remainder: ' WS-REMAINDER

           STOP RUN.

Expected Output

After DIVIDE INTO: 02500
Quotient: 03333
Quotient: 03333
Remainder: 01

Key Points

  • DIVIDE INTO: divisor INTO dividend
  • DIVIDE BY: dividend BY divisor
  • REMAINDER captures the remainder
  • ON SIZE ERROR handles division by zero