DIVIDE Giving and Remainder Example


Scenario - Divide one variable with other, result and remainder into separate variables.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DIVREMAI.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-VAR1      PIC 9(03) VALUE 32.    
          05 WS-VAR2      PIC 9(03) VALUE 5.     
          05 WS-RESULT    PIC 9(03).
          05 WS-REMAINDER PIC 9(03).

       PROCEDURE DIVISION.
           DIVIDE WS-VAR1 BY   WS-VAR2
                         GIVING WS-RESULT 
                         REMAINDER WS-REMAINDER. 

           DISPLAY "Result:      " WS-RESULT. 
           DISPLAY "Remainder:   " WS-REMAINDER.

           STOP RUN.

Output -

Result:      006
Remainder:   002

Explaining Example -

In the above example:

    Three variables declared in the working-storage section: WS-VAR1, WS-VAR2, and WS-RESULT.
  • WS-VAR1 and WS-VAR2 are initialized with the values 32 and 5 respectively. WS-RESULT is initialized to hold a numeric value with 3 digits, and WS-REMAINDER is also initialized to hold a numeric value with 3 digits.
  • It performs a division operation where WS-VAR1 is divided by WS-VAR2. The quotient of the division is stored in WS-RESULT, and the remainder of the division is stored in WS-REMAINDER.