Running Balance
Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 19 of 20.
Challenge
EasyShow what each month paid and what is left. Move the payment, or the whole balance when it is smaller, to PAID, subtract PAID from the balance, and add both to the line: Month 1 interest 10.00 paid 300.00 balance 710.00
Example input, one line per ACCEPT: 1000, 12, 300.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. LOAN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AMOUNT PIC 9(5).
01 RATE PIC 99.
01 BALANCE PIC 9(6)V99.
01 INTEREST PIC 9(4)V99.
01 OUT-INT PIC Z,ZZ9.99.
01 PAYMENT PIC 9(5).
01 MONTH-NO PIC 99 VALUE ZERO.
01 OUT-MONTH PIC Z9.
01 PAID PIC 9(6)V99.
01 OUT-PAID PIC ZZZ,ZZ9.99.
01 OUT-BAL PIC ZZZ,ZZ9.99.
PROCEDURE DIVISION.
MAIN-PARA.
ACCEPT AMOUNT.
ACCEPT RATE.
ACCEPT PAYMENT.
MOVE AMOUNT TO BALANCE.
PERFORM PAY-MONTH UNTIL BALANCE = ZERO.
STOP RUN.
PAY-MONTH.
ADD 1 TO MONTH-NO.
PERFORM CALC-INTEREST.
ADD INTEREST TO BALANCE.
*> Replace this IF so it sets PAID
IF PAYMENT < BALANCE
SUBTRACT PAYMENT FROM BALANCE
ELSE
MOVE ZERO TO BALANCE
END-IF.
MOVE MONTH-NO TO OUT-MONTH.
MOVE INTEREST TO OUT-INT.
DISPLAY "Month " OUT-MONTH
" interest " OUT-INT.
CALC-INTEREST.
COMPUTE INTEREST ROUNDED =
BALANCE * RATE / 1200.