MULTIPLY Statement


The MULTIPLY statement is used for multiplication operations. It allows multiplying one or more numeric items and storing the result in one or more receiving items.

MULTIPLY statement is divided into below types based on their usage -

  • Simple MULTIPLY statement.
  • MULTIPLY with ON SIZE ERROR (Error handling).
  • MULTIPLY with GIVING.

Simple MULTIPLY statement -


In this format, all the input numeric values or values in variables are multiplied with the output variable, and the result will be placed into the output variable.

Syntax -

---+----2----+----3----+----4----+----5
MULTIPLY input-value1|input-variable1
     BY  output-variable1 [ROUNDED]
[END-MULTIPLY].
Note! All statements coded in [ ] are optional.

Parameters -

  • input-variable1, ... - From example, it is WS-A.
  • output-variable1, ... - It is also one of the input. From example, it is WS-B.
  • ROUNDED Phrase - Used to round the fraction result to the nearest integer value based on the faction value.
  • END-MULTIPLY - Specifies the scope terminator for the MULTIPLY statement and is optional when the period is coded at the end of the MULTIPLY statement.

Example -

Scenario - Multiplying two variables.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 20.
    05 WS-B      PIC 9(03) VALUE 30.

 PROCEDURE DIVISION.
	 MULTIPLY WS-A BY WS-B.
	 DISPLAY "Result: " WS-B.

Output -

Result: 600

MULTIPLY 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 -

---+----2----+----3----+----4----+----5
MULTIPLY input-value1|input-variable1
      TO output-variable1 [ROUNDED]
       [ON SIZE ERROR statements-block1]
   [NOT ON SIZE ERROR statements-block2]
[END-MULTIPLY].

Parameters -

  • SIZE ERROR - Refer to the SIZE ERROR phrase for full information.
  • statements-block1 - specifies the set of statements that are executed when SIZE ERROR occurs. From the example, it is DISPLAY "OVERFLOW".
  • statements-block2 - specifies the set of statements that are executed when the multiply is successful. From the example, it is DISPLAY "WS-B: " WS-B.

Example -

Scenario - Multiply with error handling.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 90.
    05 WS-B      PIC 9(03) VALUE 20.

 PROCEDURE DIVISION.
	 MULTIPLY WS-A BY WS-B 
	         ON SIZE ERROR DISPLAY "OVERFLOW"
		 NOT ON SIZE ERROR DISPLAY "WS-B:  " WS-B.

Output -

OVERFLOW

MULTIPLY with GIVING -


The GIVING phrase is used when the output variables only used to store the result and not part of the inputs. All inputs before the keyword BY are multiply with inputs after the keyword BY, and the result is stored in the variables with the GIVING phrase.

Syntax -

---+----2----+----3----+----4----+----5
MULTIPLY input-value1|input-variable1
      BY input-variableA
  GIVING output-variable1 [ROUNDED]
        [ON SIZE ERROR Statements-block1]
    [NOT ON SIZE ERROR Statements-block2]
[END-MULTIPLY].

Example -

Scenario - Multiply two varibles and places the result into other variable.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 90.
    05 WS-B      PIC 9(03) VALUE 20.
    05 WS-C      PIC 9(04).

 PROCEDURE DIVISION.
	 MULTIPLY WS-A BY WS-B 
	   GIVING WS-C.
	 DISPLAY "WS-C:  " WS-C.

Output -

WS-C:  1800