COBOL Arithmetic Operations

In COBOL, Arithmetic operations are performed in PROCEDURE DIVISION. Arithmetic operations are performed using these major verbs-

1. ADD

2. SUBTRACT

3. MULTIPLY

4. DIVIDE

5. COMPUTE

ADD

It is used to perform an addition of 2 or more numeric literals or 2 or more numeric variables.

Syntax

ADD {CORRESPONDING/CORR} {Identifier1/literal1} TO {identifier2}
GIVING {Identifier3} {ROUNDED}
[ON SIZE ERROR imperative-statement-1] 
[NOT ON SIZE ERROR imperative-statement-2]END-ADD

Most used ADD -

Format/Example 1

ADD A TO B.

Here, B = A + B
If A -> 10, B -> 20
then, B becomes 30

Format/Example 2

ADD A TO B GIVING C.

Here, C = A + B
If A -> 10, B -> 20
then, C becomes 30

Format/Example 3

ADD  A  TO  B GIVING  C D.

Here,
C = A + B
D = A + B
If A -> 10, B -> 20
then, C becomes 30
D becomes 30

Format/Example 4

ADD A TO B ROUNDED.

A PIC 99V99
IF A has a value of 90.00

B PIC 99V99
IF B has a value of 10.06

C PIC 999V9
then, C should have A + B = 100.06
but C is PIC 99V9 hence it contains a value of 100.1
Here, .06 > .05 hence it is rounded to the next higher number which is .1
Consider if the value of C after addition could be 100.04, then C = 100.0
as it is rounded to next lower number as, .04 < .05

Format/Example 5

ADD A TO B ROUNDED
ON SIZE ERROR
DISPLAY “ON SIZE ERROR”.

suppose, A – PIC 9(2) – 50
B – PIC 9(2) – 60
C – PIC 9(2)

The Output of ADD Operation :
ON SIZE ERROR
ON SIZE ERROR is displayed as C = 110 but C can hold only 2 bytes maximum

Format/Example 6

ADD A TO B ROUNDED
NOT ON SIZE ERROR
DISPLAY “NOT ON SIZE ERROR”.

suppose, A – PIC 9(2) – 50
B – PIC 9(2) – 20
C – PIC 9(2)

The Output of ADD Operation :
NOT ON SIZE ERROR
NOT ON SIZE ERROR is displayed because, C = 70 and it is within the range of values
which it can hold

Sample COBOL Program to show ADD Operation

TutorialBrain-Different types of ADD Statement
Different Types of ADD operations

Output

TutorialBrain-Output of Different types of ADD Statements
Different Types of ADD operations

SUBTRACT​

 It performs the below tasks

  • This statement is used to perform subtraction of 2 or more numeric literals.
  • SUBTRACT verb is used to subtract 2 or more numeric variables.

Syntax

SUBTRACT {CORRESPONDING/CORR} {Identifier1/literal1} FROM {identifier2}
GIVING {Identifier3} {ROUNDED}
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]END-SUBTRACT

Most used SUBTRACT -

Format/Example 1

A FROM B.

Here, B = B – A
If, A -> 10 and B -> 30
then, B = 20

Format/Example 2

 A   B FROM C   D

Here, C = C-(A+B)
D = D-(A+B)
If, A -> 10, B -> 30, C -> 50 and D -> 70
then, C = 50 -(10+30) = 10
D = 70 -(10+30) = 30

Format/Example 3

A FROM B GIVING C

Here, C = B – A
If, A -> 10 and B -> 30
then, C = 20

Format/Example 4

A FROM B ROUNDED
ON SIZE ERROR
DISPLAY “ON SIZE ERROR”.

Format/Example 5

A FROM B ROUNDED
NOT ON SIZE ERROR
DISPLAY “NOT ON SIZE ERROR”.

Format/Example 6

CORRESPONDING A FROM B.

Here, The elementary subitems from group A are subtracted from group B. The values of Sub items in group A is not changed.

Sample COBOL Program to show SUBTRACT Operation

TutorialBrain-Different types of SUBTRACT Statements - Part 1
TutorialBrain-Different types of SUBTRACT Statements

Output

TutorialBrain-Output of Different types of SUBTRACT Statements
The Output

MULTIPLY​

 MULTIPLY performs the below tasks 

  • This operation is used to perform multiplication of 2 numeric literals.
  • MULTIPLY verb is used to multiply 2 numeric variables

Syntax

MULTIPLY {Identifier1/literal1} BY {identifier2}
GIVING {Identifier3}
[{ON SIZE ERROR/NOT ON SIZE ERROR} {imperative-statement}]

Most used MULTIPLY -

Format/Example 1

A BY B.

Here, B = A * B

Format/Example 2

A BY B GIVING C.

Here, C = A * B

Format/Example 3

A BY B GIVING C D.

Here, C = A * B
         D = A * B

Sample COBOL Program to show MULTIPLY Operation

TutorialBrain-Different types of MULTIPLY Statements - Part 1
TutorialBrain-Different types of MULTIPLY Statements - Part 2
Different types of MULTIPLY statement

Output

TutorialBrain-Output of the Different types of MULTIPLY Statements
The Output

DIVIDE​

 DIVIDE performs the below tasks 

  • This operation is used to perform division of 2 numeric literals.
  • DIVIDE verb is used to divide 2 numeric variables.
  • Suppose NUM1 is divided by NUM2 and there is a possibility that NUM2 can take a value of ZERO. We must handle this condition as well.

Syntax

DIVIDE {Identifier1/literal1} {INTO/BY} {identifier2}
GIVING {Identifier3}
[REMAINDER identifier4][{ON SIZE ERROR/NOT ON SIZE ERROR} {imperative-statement}]

Most used DIVIDE -

Format/Example 1

A INTO B.

Here, B = B/A

DIVIDE A BY B.  ==================>>>>>wrong<<<<<<<<

Format/Example 2

A INTO B GIVING C.

Here, C = B/A

Format/Example 3

A BY B GIVING C.

Here, C = A/B

Format/Example 4

A INTO B GIVING C D.

Here, C = B/A
         D = B/A

Format/Example 5

A BY B GIVING C D.

Here, C = A/B
         D = A/B

Format/Example 6

 A BY B GIVING C REMAINDER D.

Here, C = A/B
D = Remainder

Example: If, A = 106, B = 20
C = 106/20 = 5
D = 6

Format/Example 7

 A INTO B GIVING C REMAINDER D.

Here, C = B/A
D = Remainder
Example: If, A = 12, B = 105
C = 105/12 = 8
D = 9

Format/Example 8

A BY B GIVING C REMAINDER D
ON SIZE ERROR
MOVE 1 TO B.

Here, C = A/B
         D = Remainder

Example:

If A = 106 and  B = 0
C = 106/0 ===> Anything which is divided by zero is not defined
D = not defined.
New Value of B = 1

Sample COBOL Program to show DIVIDE Operation

TutorialBrain-Different types of DIVIDE Statements
The Output of the Different types of DIVIDE Statements

Output

The Output

COMPUTE

It is used to assign the value of the arithmetic operations which happens at the right side of ‘=’ to the variable which is present at the left side of the ‘=’.

COMPUTE can combine all the arithmetic operation and assign the result to a variable.
EQUAL Keyword is not supported in most of the COBOL versions, always use ‘=’ symbol for assigning the result.

Syntax

COMPUTE {Identifier1/literal1} [ROUNDED] = {Identifiers involving any arithmetic operations like +,-,*,/,**}
[{ON SIZE ERROR/NOT ON SIZE ERROR} {imperative-statement}]

Most used COMPUTE -

Format/Example 1

 A = B – C.

If B = 15, C = 10

Result, A = 5

Format/Example 2

D ROUNDED = A + B.

Format/Example 3

E ROUNDED = (A *B) /(D – C)

ON SIZE ERROR 

DISPLAY “ON SIZE ERROR”.

Format/Example 4

 E ROUNDED = (A *B) /(D – C)

NOT ON SIZE ERROR 

DISPLAY “NOT ON SIZE ERROR”.

Sample COBOL Program to show COMPUTE Operation

TutorialBrain-Different types of COMPUTE Statements
Different types of COMPUTE Statements

Output

TutorialBrain-Output of Different types of DIVIDE Statements
The Output

Tutorials for all brains!