COMPUTE and ROUNDED in COBOL
COMPUTE takes a whole expression at once; ROUNDED tidies the result.
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPUTE-ROUNDED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 RADIUS-M PIC 9(2)V9 VALUE 3.5.
01 AREA-SQM PIC 9(4)V99.
01 AREA-SHOW PIC ZZZ9.99.
PROCEDURE DIVISION.
*> COMPUTE takes a whole expression; ROUNDED tidies the result.
COMPUTE AREA-SQM ROUNDED = 3.14159 * RADIUS-M ** 2
MOVE AREA-SQM TO AREA-SHOW
DISPLAY "Paddock area: " AREA-SHOW " square meters"
STOP RUN.
How it works
COMPUTE ... = 3.14159 * RADIUS-M ** 2uses ordinary operators,**included.ROUNDEDrounds the final digit instead of truncating it.- The
MOVEtoZZZ9.99formats the area for theDISPLAY.
Keywords and builtins used here
COMPUTEDATADISPLAYDIVISIONIDENTIFICATIONMOVEPROCEDURESECTIONSTOP
The run, in numbers
- Lines
- 13
- Characters to type
- 410
- Tokens
- 51
- Three-star pace
- 40 tpm
At the three-star pace of 40 tokens a minute, this run takes about 76 seconds.
Step 2 of 3 in Arithmetic, step 11 of 27 in Language basics.