DIVIDE Statement


The DIVIDE statement divides one number by another. It's similar to mathematics, where we have a dividend (the number being divided) and a divisor (the number we're dividing by). The DIVIDE statement sets the output values quotient and the remainder to the corresponding variables.

DIVIDE statement has below different formats based on phrases -

  • Simple DIVIDE statement.
  • DIVIDE with INTO|BY and GIVING.
  • DIVIDE with ON SIZE ERROR (Error Handling).
  • DIVIDE with INTO|BY, GIVING and REMAINDER.
  • Simple DIVIDE statement -


    In the below format, the value of variable1 is divided by the value of variable2, and the quotient (result) is stored in variable2.

    Syntax -

    DIVIDE ws-variable1
      INTO ws-variable2 [ROUNDED]
    [END-DIVIDE].
    Note! All statements coded in [ ] are optional.

    Parameters -

    • ws-variable1 - Refers to the dividend (the number being divided). From example, it is WS-VAR1.
    • ws-variable2 - Refers to the divisor (the number we're dividing by). It is the output variable after the operation performed. From example, it is WS-VAR2.
    • ROUNDED Phrase - Used to round the fraction result to the nearest integer value based on the faction value.
    • END-DIVIDE - Specifies the scope terminator for the DIVIDE statement and is optional when the period is coded at the end of the DIVIDE statement.

    Example -

    Scenario - Divide one variable with other.

     WORKING-STORAGE SECTION.
     01 WS-VAR.
        05 WS-VAR1      PIC 9(03) VALUE 50.
        05 WS-VAR2      PIC 9(03) VALUE 5.
    
     PROCEDURE DIVISION.
    	 DIVIDE WS-VAR1 INTO WS-VAR2
    	 DISPLAY "Result: " WS-VAR2.

    Output -

    Result: 10

    DIVIDE with INTO|BY and GIVING -


    In the below format, the value of variable1 is divided by the value of variable2, and the quotient (result) is stored in variable3.

    Syntax -

    DIVIDE ws-variable1
    	INTO ws-variable2
    	GIVING ws-result
    [END-DIVIDE].

    Example -

    Scenario - Divide one variable with other and result into another variable.

     WORKING-STORAGE SECTION.
     01 WS-VAR.
        05 WS-VAR1      PIC 9(03) VALUE 50.
        05 WS-VAR2      PIC 9(03) VALUE 5.
    	05 WS-VAR3      PIC 9(03).
    
     PROCEDURE DIVISION.
    	 DIVIDE WS-VAR1 INTO WS-VAR2 
    	        GIVING WS-VAR3
    	 DISPLAY "Result: " WS-VAR3.

    Output -

    Result: 10

    DIVIDE with ON SIZE ERROR (Error handling) -


    ON SIZE ERROR is used to handle the program flow when the result value exceeds the maximum value that a result variable can hold. NOT ON SIZE ERROR phrase handles the program flow when the result value is within the range.

    Syntax -

    DIVIDE ws-variable1
      INTO ws-variable2 [ROUNDED]
    GIVING ws-result
              [ON SIZE ERROR statements-block1]
          [NOT ON SIZE ERROR statements-block2]
    [END-DIVIDE].

    Parameters -

    Example -

    Scenario - Divide with error handling.

     WORKING-STORAGE SECTION.
     01 WS-VAR.
        05 WS-VAR1      PIC 9(03) VALUE 500.
        05 WS-VAR2      PIC 9(03) VALUE 5.
    	05 WS-VAR3      PIC 9(02).
    
     PROCEDURE DIVISION.
    	 DIVIDE WS-VAR1 
    	   INTO WS-VAR2 
    	 GIVING WS-VAR3
    	        ON SIZE ERROR DISPLAY "OVERFLOW"
    			NOT ON SIZE ERROR DISPLAY "Result: " WS-VAR3.

    Output -

    OVERFLOW

    DIVIDE with INTO|BY, GIVING and REMAINDER -


    In the below format, the value of variable1 is divided by the value of variable2, the result is stored in variable3 coded with the GIVING phrase, and the remainder is stored in variable4 coded with the REMAINDER phrase.

    Syntax -

    DIVIDE ws-variable1
    	INTO ws-variable2
    	GIVING ws-result [ROUNDED]
    	[REMAINDER ws-remainder]
    	[ON SIZE ERROR statements-block1]
    	[NOT ON SIZE ERROR statements-block2]
    [END-DIVIDE].

    Parameters -

    • REMAINDER ws-remainder - Used to store store the remainder value.

    Example -

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

     WORKING-STORAGE SECTION.
     01 WS-VAR.
        05 WS-VAR1      PIC 9(03) VALUE 32.
        05 WS-VAR2      PIC 9(03) VALUE 5.
    	05 WS-VAR3      PIC 9(03) VALUE 0.
    	05 WS-VAR4      PIC 9(03) VALUE 0.
    
     PROCEDURE DIVISION.
    	 DIVIDE WS-VAR1 INTO WS-VAR2 
    	        GIVING WS-VAR3
    			REMAINDER WS-VAR4
    	 DISPLAY "Result: " WS-VAR3.
    	 DISPLAY "Remainder: " WS-VAR4.

    Output -

    Result: 6
    Remainder: 2