typestar

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

  1. COMPUTE ... = 3.14159 * RADIUS-M ** 2 uses ordinary operators, ** included.
  2. ROUNDED rounds the final digit instead of truncating it.
  3. The MOVE to ZZZ9.99 formats the area for the DISPLAY.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Arithmetic, step 11 of 27 in Language basics.

← Previous Next →