Monday 17 September 2012

arithmetics

      
                                     Arithmetic Operations in COBOL.
In COBOL we can perform arithmetic’s like Addition, Subtraction, Division and Multiplication.
           

         First we will start with Arithmetic stuff.

                             How to perform Addition operation in COBOL?
Here we can add two or more quantities.
SYNTAX:
ADD VAR-1 TO VAR-2. Or
ADD VAR-1 TO VAR-2 GIVING VAR-3.
GIVING clause is used only when you have to store the result in specific variable .
Here source and destination fields are two or more.
Ex:
ADD A TO B.
If A=10 and B=20 then result after execution is 10+20=30 but where it will stores result?
It stores result in B means destination value get overrides.
Program:
 
Added before addition code in above
OUTPUT:
New value of A is A=3+5=8
Here value of ‘C’ get override as C= 14+8+4=26

If I want to stores the addition result into multiple destination variable then I will write below program.
 
OUTPUT:

Arithmetic operation with decimal point variables.

OUTPUT:
 

                       How subtract operator works in COBOL?

Same like addition we can subtract the variables.
SYNTAX:
Subtract var-1 from var-2.or
Subtract var-1 var-2 from var-3 giving RESULT.
These will subtract var-1 and var-2 from var-3 and stores result in RESULT.

It tells compiler that subtract var-1 from var-2 and stores result in var-2.
Means destination variable get overrides.
Program:
 

 OUTPUT:
First A=50+3=53 then
D=E= 53-40=13.

We can subtract different variable from one variable only and stores result in multiple variable.

Below will not work.
 



No comments:

Post a Comment