Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Arithmetic Operations
Arithmetic Operations | Beginner | COBOL

COMPUTE Statement

Problem Description

Use COMPUTE for complex mathematical expressions.

Expected Output

Calculate compound interest using COMPUTE

Hints

COMPUTE allows mathematical expressions with +, -, *, /, **.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. COMPOUND. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-PRINCIPAL PIC 9(7)V99 VALUE 10000. 01 WS-RATE PIC 9V9(4) VALUE 0.075. 01 WS-TIME PIC 9(2) VALUE 5. 01 WS-N PIC 9(2) VALUE 4. 01 WS-AMOUNT PIC 9(9)V99. 01 WS-INTEREST PIC 9(9)V99. 01 WS-DISPLAY PIC Z,ZZZ,ZZ9.99. 01 WS-TEMP PIC 9(3)V9(6). PROCEDURE DIVISION. * Compound Interest: A = P(1 + r/n)^(nt) * Using COMPUTE with ** for exponentiation COMPUTE WS-TEMP = 1 + (WS-RATE / WS-N). COMPUTE WS-AMOUNT = WS-PRINCIPAL * (WS-TEMP ** (WS-N * WS-TIME)). COMPUTE WS-INTEREST = WS-AMOUNT - WS-PRINCIPAL. DISPLAY "=== COMPOUND INTEREST CALCULATOR ===". MOVE WS-PRINCIPAL TO WS-DISPLAY. DISPLAY "PRINCIPAL: " WS-DISPLAY. DISPLAY "RATE: 7.5%". DISPLAY "TIME: " WS-TIME " YEARS". DISPLAY "COMPOUNDED: QUARTERLY". MOVE WS-AMOUNT TO WS-DISPLAY. DISPLAY "FINAL AMOUNT: " WS-DISPLAY. MOVE WS-INTEREST TO WS-DISPLAY. DISPLAY "INTEREST: " WS-DISPLAY. STOP RUN.

Explanation:

COMPUTE allows complex formulas. ** is exponentiation. Use parentheses for order.