| Q. How do you perform Arithmetic in COBOL? For example, how do you write a simple program that calculates the Interest to be paid on a Principal p? |
You often need to do Math in COBOL. You can perform Arithmetic in COBOL using Arithmetic Operators. When you perform some arithmetic on two or more numeric quantities, such an expression is called an Arithmetic Expressions. There are 4 main Arithmetic Operators in COBOL – ADD, SUBTRACT, MULTIPLY and DIVIDE. You can even write complex arithmetic expressions by using the COMPUTE verb. I have written about the syntax of each operator, followed by examples and some results.
|
| Q. How’z the ADD Operator used in COBOL. Show me some stuff... |
| The ADD operator can be used to add two or more quantities. In the assembly language, we write the instruction ADD A B to find the sum of A and B. The final sum is stored in A. On the same lines, the ADD Operator in COBOL has the following syntax : Syntax ADD one-quantity TO second-quantity The final sum result is stored in the second-variable. For example, if A=10, and I write ADD 5 TO A After performing the above step, the computer adds 5 to the value of A, so A=15. Suppose a variable B=5, then I may also write ADD B TO A
Syntax ADD A B C TO SUM In this case, all the three group of numbers A B and C, would be added to SUM. So, assuming A=10, B=15, and C=20, if initially RESULT = 100, after addition of A, B and C to SUM, the value of RESULT = 100 + (10+15+20) = 145. Example
Result
Syntax ADD A B GIVING RESULT If you would like the final result of arithmetic to be stored in a separate resultant variable, you can always write a GIVING clause at the end. Thus, if A=5 and B=10, the sum(addition of A and B), would yield 15. This sum 15 is stored in the RESULT Variable. On omitting the GIVING Clause, the sum is stored in one of the operands being added. GIVING Clause allows you to specify a separate destination(receiving variable) which shall hold the final result of computation. Example
Result
Syntax ADD A B C GIVING D E The GIVING Clause may also specify a list of destination/receiving variables. In that case, sum(addition) is copied to the each of the variables mentioned in the GIVING Clause. For example, Assume that A = 10, B = 15 and C=20. Now the sum of A B C = 10 + 15 + 20 = 45. The sum 45 is stored in each of the variables mentioned in the list(D, E). Thus, D = 45 and E = 45, after the above statement is executed. Example :
Result :
Comparing GIVING Clause with TO Clause : Suppose RESULT = 100. ADD 10 20 GIVING RESULT. After the operation, RESULT = 30 (Old value is overwritten with the new value) ADD 10 20 TO RESULT. After the operation, RESULT = 130 (Old value is merely added to the operands, to arrive at the new value)
|
| Q. How does the SUBTRACT Operator work in COBOL? |
| When you want to find the difference of two or more quantities in COBOL, you use the SUBTRACT operator. The SUBTRACT operator subtracts one quantity from another, and finds out the result. In its simplest form, SUBTRACT has the following syntax : Syntax SUBTRACT one-quantity FROM second-quantity For example, SUBTRACT WS-VAR-2 FROM WS-VAR-1 Consider the variable WS-VAR-1 = 10, WS-VAR-2=30, subtracting WS-VAR-2 from WS-VAR-1 would result in the difference between 30 and 10. The answer is 20. This final result is stored in WS-VAR-2. SUBTRACT B FROM A is equivalent to the statement A = A – B.
The example above is shown in the code snippet below :
The output of the above code snippet would look like this :
However, at times, you would like to store the difference between these two quantities in a separate third variable. To do so, you would put the following syntax to use : Syntax SUBTRACT variable-1 FROM variable-2 GIVING variable-3 For example, you would write SUBTRACT WS-VAR-1 FROM WS-VAR-2 GIVING WS-VAR-3, with WS-VAR-1=30, WS-VAR-2=10, you get WS-VAR-3 = WS-VAR-2 minus WS-VAR-1, i.e. WS-VAR-3 = 30 – 10 = 20. Consider the code snippet given below :
Upon running the above code snippet, you would get the following output :
COBOL also gives you the facility to subtract a group of numbers from a given quantity. For example, let’s assume that you want to subtract a group of numbers 4 5 1 from a given sum 25. You compute the result as follows : 25 - (4 + 5 + 1) = 15. In COBOL you would write it as, SUBTRACT 4 5 1 FROM 25 Syntax SUBTRACT item-1 item-2 item-3 ... FROM SUM [GIVING OUTPUT] The example below shows, how to use this syntax in COBOL
The output of the above code-snippet, when you would run, should look like this -
|
| Q. How does MULTIPLY Verb compute the product of two numbers in COBOL? |
| The MULTIPLY Verb is used to multiply two quantities together, that results in a Product. Let us assume the following data-variables, that’ll help us to understand the example.
The general form of MULTIPLY Verb is : Syntax: MULTIPLY number-1 BY number-2 This simple statement, multiplies the number-2 by number-1. For example, if A = 5, MULTIPLY 2 BY A, would result in A getting doubled, so the new value of A=10.
Result
Syntax: MULTIPLY number-1 BY number-2 GIVING result As you know, the GIVING Clause specifies a separate destination or receiving variable. So, the product is stored in the GIVING Clause variable result. For example, if A = 10, B = 20, MULTIPLY A BY B GIVING C, results in 10 x 20 = 200 getting stored in C. Example
Result
Syntax: MULTIPLY A BY B C D This format of MULTIPLY Statement specifies that, A is to be multiplied each of the variables B, C and D, so that it results in the products A x B, A x C, A x D. The final resulting three products are stored in B, C and D itselves. If A = 10, and B = 2, C = 3 and D = 4, after the executing this MULTIPLY instruction, B, C and D become ten-folds, B = 20, C=30 and D=40. Example
Result
Syntax: MULTIPLY A BY B C GIVING D E In this form of the MULTIPLY statement, as you would guess there would be 2 products : A x B (A times B) and A x C(A times C). These final products would be stored in separate receiving variables D and E, as specified by the GIVING Clause.
|
| Q. How does DIVIDE Verb divide a one operand by another? How do you find the remainder(what’s left over)? |
| The DIVIDE verb in COBOL, is used compute the quotient, upon the division of two numbers. The DIVIDE Verb has several forms available. DIVIDE A INTO B Here, the DIVIDE Operator is used to perform simple Division. You divide B into ‘A’ equal parts, how many parts do you get? In other words, this statement calculates the fraction B/A. Assume that you have a chocolate that’s 100 length. If you were to divide it into 5 cm long bars, you would get 100/5 = 20 bars to eat. To do this you would write, DIVIDE 5 INTO 100 DIVIDE A INTO B C You may also divide the multiple dividends(operands) by the same divisor, using a single statement. Suppose A=2, and B=58,C=156. Then, DIVIDE A INTO B C would mean that you are trying to divide 2 into 58 and 156. So, both 58 and 156 would be halved to 29 and 78. DIVIDE A INTO B C GIVING D E GIVING Clause can be used optionally to specify separate receiving/destination variables for storing the results. DIVIDE B BY A GIVING C You can also use the BY Clause to specify the dividend first and then the divisor.
|