Skip to content

DIVIDE Statement

The DIVIDE statement performs arithmetic division, allowing for the storage of the quotient, and the remainder.

Format 1

The Format 1 DIVIDE Statement DIVIDE's a numeric-data-1 INTO a numeric-2 data item, or multiple numeric-2 data items, and moves the result of the DIVIDE operation into the numeric-2 (or multiple numeric-2) data items. The result may be optionally ROUNDED. Remainder is not stored.

       DIVIDE numeric-data-1 INTO { numeric-2 [ROUNDED] } ... 
              [ ON SIZE ERROR statement-1 ] 
              [ NOT ON SIZE ERROR statement-2 ] 
              [ END-DIVIDE ]

Format 2

The Format 2 DIVIDE Statement DIVIDE's a numeric-data-1 INTO a numeric-data-2, and moves the result of the DIVIDE operation one or more numeric-3 data items, named as the target(s) of a GIVING clause. The result may be optionally ROUNDED. Remainder is not stored.

       DIVIDE numeric-data-1 INTO numeric-data-2 
                     GIVING { numeric-3 [ROUNDED] } ... 
              [ ON SIZE ERROR statement-1 ] 
              [ NOT ON SIZE ERROR statement-2 ] 
              [ END-DIVIDE ]

Format 3

The Format 3 DIVIDE Statement DIVIDE's a numeric-data-1 BY a numeric-data-2, and moves the result of the DIVIDE operation one or more numeric-3 data items, named as the target(s) of a GIVING clause. The result may be optionally ROUNDED. Remainder is not stored.

       DIVIDE numeric-data-1 BY numeric-data-2 
                     GIVING { numeric-3 [ROUNDED] } ... 
              [ ON SIZE ERROR statement-1 ] 
              [ NOT ON SIZE ERROR statement-2 ] 
              [ END-DIVIDE ]

Format 4

The Format 4 DIVIDE Statement DIVIDE's a numeric-data-1 INTO a numeric-data-2, and moves the result of the DIVIDE a numeric-3 data item, named as the target of a GIVING clause. The result may be optionally ROUNDED. Remainder is stored in a numeric-4 data item.

       DIVIDE numeric-data-1 INTO numeric-data-2 
                     GIVING numeric-3 [ROUNDED] 
                     REMAINDER numeric-4 
              [ ON SIZE ERROR statement-1 ] 
              [ NOT ON SIZE ERROR statement-2 ] 
              [ END-DIVIDE ]

Format 5

The Format 5 DIVIDE Statement DIVIDE's a numeric-data-1 item BY a numeric-data-2 item , and moves the result of the DIVIDE operation into a numeric-3 data item, named as the target of a GIVING clause. The result may be optionally ROUNDED. Remainder is stored in a numeric-4 data item.

       DIVIDE numeric-data-1 BY numeric-data-2 
                     GIVING numeric-3 [ROUNDED] 
                     REMAINDER numeric-4 
              [ ON SIZE ERROR statement-1 ] 
              [ NOT ON SIZE ERROR statement-2 ] 
              [ END-DIVIDE 

Syntax:

  1. numeric-n is a literal or data item that is numeric.
  2. numeric-data-n is a numeric data item.
  3. statement-n is an imperative statement.

General Rules:

  1. Format 1, Format 2, and Format 3 of the DIVIDE operation allow multiple receiving fields, but do not allow for the storing of the REMAINDER.
  2. Format 4 and Form at 5 of the DIVIDE operation only allow a single receiving field, but also allow for the storing of the REMAINDER.
  3. The ROUNDED clause is applied when a DIVIDE operation produces a result that includes more decimal places than are included in the description of the data item given to hold the final result of the arithmetic operation.
  4. For rules regarding the ROUNDED clause, see the entry for ROUNDED in the Common General Rules section.
  5. The SIZE ERROR exception is triggered if the ON SIZE ERROR clause is present and if the receiving field is not large enough to accommodate the result of the DIVIDE function.
  6. The SIZE ERROR exception is triggered if numeric-data-1 has a value of zero.
  7. For rules regaring the ON SIZE ERROR clause, see the entry for ON SIZE ERROR in the Common General Rules section.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. DIVIDE-1. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
       78 CONST-1           VALUE 10. 
       78 CONST-2           VALUE 25. 
       77 NUMERIC-1         PIC 99. 
       77 NUMERIC-2         PIC 99. 
       77 NUMERIC-3         PIC 9V99. 
       77 NUMERIC-4         PIC 9V99. 
       77 DUMMY             PIC X. 
       PROCEDURE DIVISION.
       MAIN. 
      *FORMAT 1 
      *DIVIDE NUMERIC-1 INTO { NUMERIC-2 [ROUNDED] } ... 
      * [ ON SIZE ERROR STATEMENT-1 ] 
      * [ NOT ON SIZE ERROR STATEMENT-2 ] 
      * [ END-DIVIDE ] 

              MOVE CONST-1 TO NUMERIC-1. 
              MOVE CONST-2 TO NUMERIC-2. 

              DIVIDE NUMERIC-1 INTO NUMERIC-2 ROUNDED 
                     ON SIZE ERROR 
                            DISPLAY "FORMAT 1 SIZE ERROR!" LINE 3 COL 10 
                     NOT ON SIZE ERROR 
                            DISPLAY NUMERIC-2 LINE 3 COL 10 
              END-DIVIDE. 

      *FORMAT 2 
      * DIVIDE NUMERIC-1 INTO NUMERIC-2 
      * GIVING { NUMERIC-3 [ROUNDED] } ... 
      * [ ON SIZE ERROR STATEMENT-1 ] 
      * [ NOT ON SIZE ERROR STATEMENT-2 ] 
      * [ END-DIVIDE ] 
      * 

              MOVE CONST-1 TO NUMERIC-1. 
              MOVE CONST-2 TO NUMERIC-2. 

              DIVIDE NUMERIC-1 INTO NUMERIC-2 
                     GIVING NUMERIC-3 ROUNDED 
                     ON SIZE ERROR 
                            DISPLAY "FORMAT 2 SIZE ERROR!" LINE 4 COL 10 
                     NOT ON SIZE ERROR 
                            DISPLAY NUMERIC-3 LINE 4 COL 10 
              END-DIVIDE. 

      *FORMAT 3 
      * DIVIDE NUMERIC-1 BY NUMERIC-2 
      *       GIVING { NUMERIC-3 [ROUNDED] } ... 
      * [ ON SIZE ERROR STATEMENT-1 ] 
      * [ NOT ON SIZE ERROR STATEMENT-2 ] 
      * [ END-DIVIDE ] 
      * 

              MOVE CONST-1 TO NUMERIC-1. 
              MOVE CONST-2 TO NUMERIC-2. 

              DIVIDE NUMERIC-1 BY NUMERIC-2 
                     GIVING NUMERIC-3 ROUNDED 
                     ON SIZE ERROR 
                            DISPLAY "FORMAT 3 SIZE ERROR!" LINE 5 COL 10 
                     NOT ON SIZE ERROR 
                            DISPLAY NUMERIC-3 LINE 5 COL 10 
              END-DIVIDE. 

      *FORMAT 4 
      * DIVIDE NUMERIC-1 INTO NUMERIC-2 
      *       GIVING NUMERIC-3 [ROUNDED] 
      *       REMAINDER NUMERIC-4 
      * [ ON SIZE ERROR STATEMENT-1 ] 
      * [ NOT ON SIZE ERROR STATEMENT-2 ] 
      * [ END-DIVIDE ] 
      * 

              MOVE CONST-1 TO NUMERIC-1. 
              MOVE CONST-2 TO NUMERIC-2.

              DIVIDE NUMERIC-1 INTO NUMERIC-2 
                     GIVING NUMERIC-3 ROUNDED 
                     REMAINDER NUMERIC-4 
                     ON SIZE ERROR 
                            DISPLAY "FORMAT 4 SIZE ERROR!" LINE 6 COL 10 
                     NOT ON SIZE ERROR 
                            DISPLAY NUMERIC-3 LINE 6 COL 10 
                            DISPLAY "REMAINDER" LINE 6 COL 20 
                            DISPLAY NUMERIC-4 LINE 6 COL 30 
                     END-DIVIDE. 

      *FORMAT 5 
      * DIVIDE NUMERIC-1 BY NUMERIC-2 
      * GIVING NUMERIC-3 [ROUNDED] 
      * REMAINDER NUMERIC-4 
      * [ ON SIZE ERROR STATEMENT-1 ] 
      * [ NOT ON SIZE ERROR STATEMENT-2 ] 
      * [ END-DIVIDE ] 

              MOVE CONST-1 TO NUMERIC-1. 
              MOVE CONST-2 TO NUMERIC-2. 

              DIVIDE NUMERIC-1 BY NUMERIC-2 
                     GIVING NUMERIC-3 ROUNDED 
                     REMAINDER NUMERIC-4 
                     ON SIZE ERROR 
                            DISPLAY "FORMAT 5 SIZE ERROR!" LINE 7 COL 10 
                     NOT ON SIZE ERROR 
                            DISPLAY NUMERIC-3 LINE 7 COL 10 
                            DISPLAY "REMAINDER" LINE 7 COL 20 
                            DISPLAY NUMERIC-4 LINE 7 COL 30 
              END-DIVIDE. 

              DISPLAY "DIVIDE-1 FINISHED!" LINE 10 COL 10. 
              ACCEPT DUMMY LINE 10 COL 30. 

              STOP RUN.
Back to top