ARITHMETIC OPERATORS IN COBOL

To go with any operation, where we have to do any calculation or to solve complex equations, we need some arithmetic operators. To do this, COBOL provides us with a set of arithmetic operators to help us in our calculations.

A set of arithmetic operators provided by COBOL is given below:

  • add
  • subtract
  • multiply
  • divide
  • compute

ADD

This operator is used for addition.

SYNTAX=>

EXAMPLE=>

1) ADD A TO B

a – 10 , b – 20 , b comes 30

2) ADD CORRESPONDING A TO B.

3) ADD A TO B GIVING C.

a – 10 , b – 20 , then c = 10 + 20 = 30

also

ADD A B GIVING C D

C = A + B

AND D = A + B

4) ADD A TO B ROUNDED.

A – 90.00

B – 10.06

c = 100.06 9(3)V9 THEN 100.1

5) ADD A TO B ROUNDED

ON SIZE ERROR

DISPLAY ” ON SIZE ERROR “.

a – pic 9(2) – 50

b – pic 9(2) – 60

b – 110

6) ADD A TO B ROUNDED

NOT ON SIZE ERROR

DISPLAY ” NOT ON SIZE ERROR “

SAMPLE PROGRAM –

This is a demo program to learn and understand the concept of addition operator.

THE OUTPUT WILL BE

COBOL Arithmetic Operators

In the above program, we have assigned few variables with different values, and later manipulated the output using different addition operators to clarify the different concepts related to addition operators.

SUBTRACT

This operator is used for SUBTRACTION.

SYNTAX=>

EXAMPLE=>

1) SUBTRACT A FROM B

B = B – A

2) SUBTRACT A FROM B GIVING C.

C = B – A

3) SUBTRACT CORRESPONDING A FROM B.

4) SUBTRACT A B FROM C.

IT IS LIKE C = C – ( A + B )

A = 10

B = 20

C = 50

C = 50 – ( 10 + 20 ) = 20

4) SUBTRACT A TO B ROUNDED.

A – 90.00

B – 10.06

c = 80.06 9(3)V9 THEN 80.1

5) SUBTRACT A TO B ROUNDED

ON SIZE ERROR

DISPLAY ” ON SIZE ERROR “.

6) SUBTRACT A TO B ROUNDED

NOT ON SIZE ERROR

DISPLAY ” NOT ON SIZE ERROR “

SAMPLE PROGRAM –

This is a demo program to learn and understand the concept of subtraction operator.

The output will be

COBOL Arithmetic Operators

Here in this program, we have assigned few variables with different values and later manipulated the output using different Subtraction operators to clarify the different concepts related to this operator.

MULTIPLY

This operator is used for multiplication of one number to another.

SYNTAX=>

EXAMPLE=>

A) MULTIPLY A BY B

B = A * B

B) MULTIPLY A BY B GIVING C.

C = A * B

C) MULTIPLY A BY B GIVING C D.

C = A * B

D = A * B

SAMPLE PROGRAM –

This is a demo program to learn and understand the concept of multiplication operator and how different types of multiplication operator works.

The output will be

COBOL Arithmetic Operators

Here in this program, we have assigned few variables with different values and later manipulated the output using different Multiplication operators to clarify the different concepts related to Multiplication operators.

—————————————————————————-

DIVISION

This operator is used for the division of one number by another

SYNTAX=>

EXAMPLE=>

A) DIVIDE A INTO B.

B = B / A

B) DIVIDE A BY B.

A = A / B.

{It is Wrong; never give a command like this, output will show compilation error. }

C) DIVIDE A INTO B BY GIVING C.

C = B / A

D) DIVIDE A BY B GIVING C.

C = A / B

E) DIVIDE A INTO B BY GIVING C D.

C = B / A

D = B / A

F) DIVIDE A BY B GIVING C D.

C = A / B

D = A / B

G) DIVIDE A BY B GIVING C REMAINDER D.

C = A / B

Example= if A = 106 , B = 0

C = 106 / 20 = 5

D = 6

H) DIVIDE A INTO B GIVING C REMAINDER D.

C = B / A

Example= if A = 12 , B = 20

C = 105 / 12 = 8

D = 9

I) DIVIDE A BY B GIVING C REMAINDER D.

ON SIZE ERROR

MOVE 1 TO B.

C = A / B

Example= if A = 106 , B = 0

C = 106 / 0 => anything which is divided by zero is not defined

D = not defined

SAMPLE PROGRAM –

This is a demo program to learn and understand the concept of division operators.

The output will be

COBOL Arithmetic Operators

Here in this program, we have assigned few variables with different values and later manipulated the output using different division operators to clarify the different concepts related to division operators.

COMPUTE

COMPUTE is used to assign the value of arithmetic operators present at the right-hand side of ” = ” to the literal/variable that is present at the left-hand side of ” = “. Moreover, compute can combine and solve all the arithmetic operators and assign the result to a single variable.

SYNTAX =>

EXAMPLE =>

B = 15 , C =10

1) COMPUTE A = B – C

RESULT , A = 5

2) COMPUTE D ROUNDED = A + B.

RESULT , D = 20

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

NOT IN SIZE ERROR

DISPLAY ” NOT ON SIZE ERROR “.

RESULT, E = ( 5 * 15 ) / ( 20 – 10 ) = 75 / 10 = 7

4) COMPUTE F ROUNDED = B * C

ON SIZE ERROR

DISPLAY ” ON SIZE ERROR “.

RESULT, F =15 * 10 = 150

SAMPLE PROGRAM –

This is a demo program to learn and understand the concept of compute operator and how different types of compute operator works.

THE OUTPUT WILL BE

COBOL Arithmetic Operators

Here in this program, we have assigned few variables with different values and later manipulated the output using different compute operators to clarify the different concepts related to compute operators.

source