More Arithmetic Operations - Multiplication and Division

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.

What You'll Learn

In this lesson, you will learn:

  1. Multiplication:

    • Using the MULTIPLY statement to multiply numeric values.
  2. Division:

    • Using the DIVIDE statement to divide numeric values and how to handle remainders.

Let's see how these operations work in COBOL with an example:

IDENTIFICATION DIVISION.
PROGRAM-ID. ArithmeticOperations.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Value1 PIC 9(3).
01 Value2 PIC 9(3).
01 Result PIC 9(5).
01 Result-Remainder PIC 9(3).

PROCEDURE DIVISION.
    MOVE 10 TO Value1.
    MOVE 480 TO Value2.

    *> MULTIPLY operation
    MULTIPLY Value1 BY Value2 GIVING Result.
    DISPLAY "The result of multiplication is: " Result. *> 4800

    *> DIVIDE operation
    DIVIDE Value2 BY Value1 GIVING Result.
    DISPLAY "The result of division is: " Result. *> 48

    *> DIVIDE operation with non-zero remainder
    DIVIDE 485 BY Value1 GIVING Result REMAINDER Result-Remainder.
    DISPLAY "The result of division with remainder is: " Result. *> 48
    DISPLAY "Remainder of division is: " Result-Remainder. *> 5

    STOP RUN.
Breaking Down the Code
  1. Multiplication:

    • The MULTIPLY statement multiplies Value1 by Value2 and stores the result in Result:
      MULTIPLY Value1 BY Value2 GIVING Result.
  2. Division:

    • The DIVIDE statement divides Value2 by Value1 and stores the result in Result:

      DIVIDE 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:

      DIVIDE 485 BY Value1 GIVING Result REMAINDER Result-Remainder.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal