The DIVIDE statement in COBOL performs division operations, dividing one number by another and storing the result. DIVIDE is one of the fundamental arithmetic statements in COBOL, along with ADD, SUBTRACT, and MULTIPLY. Understanding DIVIDE is essential for performing calculations, financial computations, and any arithmetic operations requiring division.
DIVIDE supports various formats including DIVIDE INTO and DIVIDE BY, remainder calculation with REMAINDER clause, overflow detection with ON SIZE ERROR, and multiple division operations. Whether you're calculating averages, percentages, rates, or performing financial calculations, mastering DIVIDE is crucial for COBOL programming.
12345678910DIVIDE divisor INTO dividend GIVING result [REMAINDER remainder] [ON SIZE ERROR statements] END-DIVIDE *> Or using BY DIVIDE dividend BY divisor GIVING result [REMAINDER remainder] [ON SIZE ERROR statements] END-DIVIDE
12345678910111213141516WORKING-STORAGE SECTION. 01 DIVIDEND PIC 9(5) VALUE 100. 01 DIVISOR PIC 9(3) VALUE 4. 01 QUOTIENT PIC 9(5)V99. PROCEDURE DIVISION. MAIN-LOGIC. *> Divide 100 by 4 = 25 DIVIDE DIVISOR INTO DIVIDEND GIVING QUOTIENT DISPLAY "Result: " QUOTIENT *> Using BY syntax DIVIDE DIVIDEND BY DIVISOR GIVING QUOTIENT DISPLAY "Result: " QUOTIENT STOP RUN.
123456789101112131415WORKING-STORAGE SECTION. 01 DIVIDEND PIC 9(5) VALUE 23. 01 DIVISOR PIC 9(3) VALUE 7. 01 QUOTIENT PIC 9(5). 01 REMAINDER-VALUE PIC 9(3). PROCEDURE DIVISION. MAIN-LOGIC. DIVIDE DIVISOR INTO DIVIDEND GIVING QUOTIENT REMAINDER REMAINDER-VALUE *> 23 / 7 = 3 remainder 2 DISPLAY "Quotient: " QUOTIENT DISPLAY "Remainder: " REMAINDER-VALUE STOP RUN.
123456789101112131415WORKING-STORAGE SECTION. 01 LARGE-NUMBER PIC 9(5) VALUE 99999. 01 SMALL-DIVISOR PIC 9(2) VALUE 1. 01 RESULT PIC 9(3). PROCEDURE DIVISION. MAIN-LOGIC. DIVIDE SMALL-DIVISOR INTO LARGE-NUMBER GIVING RESULT ON SIZE ERROR DISPLAY "ERROR: Result overflow" MOVE 999 TO RESULT *> Set to maximum END-DIVIDE DISPLAY "Result: " RESULT STOP RUN.
123456789101112131415161718WORKING-STORAGE SECTION. 01 DIVIDEND PIC 9(5) VALUE 100. 01 DIVISOR PIC 9(3) VALUE 0. 01 QUOTIENT PIC 9(5). PROCEDURE DIVISION. MAIN-LOGIC. IF DIVISOR = 0 DISPLAY "ERROR: Division by zero not allowed" MOVE 0 TO QUOTIENT ELSE DIVIDE DIVISOR INTO DIVIDEND GIVING QUOTIENT ON SIZE ERROR DISPLAY "ERROR: Result overflow" END-DIVIDE END-IF STOP RUN.
123456789101112131415161718192021222324252627282930313233343536IDENTIFICATION DIVISION. PROGRAM-ID. DIVIDE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-SALES PIC 9(8)V99 VALUE 100000.00. 01 NUMBER-OF-REGIONS PIC 9(2) VALUE 5. 01 AVERAGE-SALES PIC 9(6)V99. 01 PERCENTAGE PIC 9(3)V99. 01 REGION-SALES PIC 9(6)V99 VALUE 15000.00. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Division Calculations ===" *> Calculate average IF NUMBER-OF-REGIONS = 0 DISPLAY "ERROR: Cannot divide by zero" ELSE DIVIDE NUMBER-OF-REGIONS INTO TOTAL-SALES GIVING AVERAGE-SALES ON SIZE ERROR DISPLAY "ERROR: Average calculation overflow" END-DIVIDE DISPLAY "Average sales per region: $" AVERAGE-SALES END-IF *> Calculate percentage IF TOTAL-SALES = 0 DISPLAY "ERROR: Cannot calculate percentage with zero total" ELSE COMPUTE PERCENTAGE = (REGION-SALES / TOTAL-SALES) * 100 DISPLAY "Region sales as percentage: " PERCENTAGE "%" END-IF STOP RUN.
Think of DIVIDE like sharing cookies:
So DIVIDE is like sharing things equally and seeing how much each person gets!
1. What does DIVIDE do?
2. What is the REMAINDER clause used for?
3. What should you do before dividing?
4. What is ON SIZE ERROR used for?