Welcome back to our COBOL programming course! In the last lesson, you learned how to perform simple arithmetic operations like addition and subtraction. Now, it's time to expand on that knowledge and explore more arithmetic operations — specifically multiplication and division. Understanding these operations is essential, as they are commonly used in business applications for tasks such as financial calculations and data analysis.
In this lesson, you will learn:
Multiplication:
MULTIPLY
statement to multiply numeric values.Division:
DIVIDE
statement to divide numeric values and how to handle remainders.Let's see how these operations work in COBOL with an example:
cobol1IDENTIFICATION DIVISION. 2PROGRAM-ID. ArithmeticOperations. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Value1 PIC 9(3). 601 Value2 PIC 9(3). 701 Result PIC 9(5). 801 Result-Remainder PIC 9(3). 9 10PROCEDURE DIVISION. 11 MOVE 10 TO Value1. 12 MOVE 480 TO Value2. 13 14 *> MULTIPLY operation 15 MULTIPLY Value1 BY Value2 GIVING Result. 16 DISPLAY "The result of multiplication is: " Result. *> 4800 17 18 *> DIVIDE operation 19 DIVIDE Value2 BY Value1 GIVING Result. 20 DISPLAY "The result of division is: " Result. *> 48 21 22 *> DIVIDE operation with non-zero remainder 23 DIVIDE 485 BY Value1 GIVING Result REMAINDER Result-Remainder. 24 DISPLAY "The result of division with remainder is: " Result. *> 48 25 DISPLAY "Remainder of division is: " Result-Remainder. *> 5 26 27 STOP RUN.
Multiplication:
MULTIPLY
statement multiplies Value1
by Value2
and stores the result in Result
:
cobol1MULTIPLY Value1 BY Value2 GIVING Result.
Division:
The DIVIDE
statement divides Value2
by Value1
and stores the result in Result
:
cobol1DIVIDE Value2 BY Value1 GIVING Result.
Handling remainders is an important part of division. In the example above, we divide 485 by 10. The GIVING
clause stores the quotient in Result
, and the REMAINDER
clause stores the remainder in Result-Remainder
:
cobol1DIVIDE 485 BY Value1 GIVING Result REMAINDER Result-Remainder.
Being proficient in multiplication and division operations is crucial because these operations are pervasive in so many areas of business and technical applications. By mastering these operations, you'll be better equipped to:
Ready to apply what you've learned? Let's jump into the practice section and put your new skills to the test!