MULTIPLY On Size Error Example


Scenario - Multiply two numbers and store the result in another variable.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MULTERR.
       AUTHOR. MTH.
 
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-V1         PIC 9(02) VALUE 30.
          05 WS-V3         PIC 9(02) VALUE 40.
          05 WS-V4         PIC 9(03).

       PROCEDURE DIVISION.

      *    MULTIPLY WITH ON SIZE ERROR
           MULTIPLY WS-V1   BY WS-V3
             GIVING WS-V4
                 ON SIZE ERROR DISPLAY "RESULT OVERFLOW"
             NOT ON SIZE ERROR
                DISPLAY "Result: " WS-V4.

           STOP RUN.

Output -

RESULT OVERFLOW

Explaining Example -

In the above example:

  • It declares three variables: WS-V1 with a value of 30, WS-V3 with a value of 40, and WS-V4.
  • It performs a multiplication operation on WS-V1 and WS-V3, and the overflow occurs during multiplication.
  • It displays "RESULT OVERFLOW". If no overflow occurs, it displays the result stored in WS-V4.