REMAINDER
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 15 of 23.
DIVIDE into a whole-number field drops the fraction. REMAINDER keeps what is left over, in a field of its own:
DIVIDE 17 BY 5 GIVING Q REMAINDER R.
*> Q is 3, R is 2The remainder is the dividend minus the quotient times the divisor: 17 minus 3 times 5 is 2. COBOL works it out from the quotient as stored, so give the quotient no decimal places.
Remainders split amounts into units: 135 minutes are 2 hours and 15 minutes.
DIVIDE MINUTES BY 60 GIVING HRS
REMAINDER MINS.A remainder of 0 after dividing by 2 means the number is even; 1 means it is odd.
Challenge
EasyRead MINUTES and split it into HRS and MINS with one DIVIDE and REMAINDER, so 135 prints 02h 15m.
Example input, one line per ACCEPT: 135.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. HOURS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MINUTES PIC 9(4).
01 HRS PIC 99.
01 MINS PIC 99.
PROCEDURE DIVISION.
ACCEPT MINUTES.
*> Split MINUTES into HRS and MINS
DISPLAY HRS "h " MINS "m".
STOP RUN.