HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to COBOLVariablesArithmeticConditions (IF)Loops (PERFORM)
REFERENCE
📖 Overview
🖥️ Structure & Output
📦 Variables
🔢 Arithmetic
🔀 Condition
🔁 Loop
COBOL›Arithmetic

Arithmetic

Calculation with COMPUTE and remainder with FUNCTION MOD. (Numbers are padded to fit the PIC definition.)

Simple enough for a 5-year-old to understand

COMPUTE

COMPUTE calculates a formula and writes the result to a variable. The result variable's PIC determines the number of digits.

Example code
1
IDENTIFICATION DIVISION.
2
PROGRAM-ID. H.
3
DATA DIVISION.
4
WORKING-STORAGE SECTION.
5
01 R PIC 9.
Single-digit result.
VariableSingle-digit result.
6
PROCEDURE DIVISION.
7
COMPUTE R = 3 + 4.
Calculate.
CodeCalculate.
8
DISPLAY R.
7
Output / Input7
9
STOP RUN.
VariableCodeOutput / Input
💡 Hint: PIC 9 is a single digit; PIC 9(3) is three digits (7 → 007). Size the result according to PIC.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Calculate the remainder of dividing 23 by 4 using FUNCTION MOD into R and DISPLAY it.

🎯 Expected output: 3

Loading editor…

About this topic

COBOL arithmetic can be written in two forms: verb-based commands and mathematical expression syntax. The verb form resembles an English sentence, while the expression form allows you to write the formula directly.

COBOL's arithmetic is decimal-based. Floating-point rounding issues do not exist here: if you specify how many decimal places you want in the picture clause, the calculation is performed to that exact precision. This is one of the technical reasons why financial software remains in COBOL.

Conversely, when the result field is too narrow, the excess is silently truncated. This applies to both the integer and decimal parts, and no error message is generated; separate clauses must be used to catch this.

What you will learn

  • Arithmetic verbs
  • Expression-based calculation
  • The rounding clause
  • Size error checking
  • Decimal place behavior

Who is this for?

  • Those writing financial software
  • Those dealing with amount shifts
  • Those reviewing legacy accounting code

When to use?

In formulas combining multiple operations, the expression form is both concise and readable; for simple single-operation updates, the verb form is sufficient.

In any calculation involving decimal division, the rounding clause must be explicitly written; otherwise, cents are systematically truncated downward and totals diverge.

Common mistakes

Wrong

01 WS-SONUC PIC 9(3)V9(2) VALUE ZERO.
PROCEDURE DIVISION.
    COMPUTE WS-SONUC = 2 / 3.
    DISPLAY WS-SONUC.
    STOP RUN.

Since rounding was not requested, everything after the second decimal place is truncated; the result becomes zero point sixty-six, not sixty-seven. In a single operation, the difference is one cent, but across millions of records, the total systematically skews downward because truncation always works in the same direction.

Correct

01 WS-SONUC PIC 9(3)V9(2) VALUE ZERO.
PROCEDURE DIVISION.
    COMPUTE WS-SONUC ROUNDED = 2 / 3.
    DISPLAY WS-SONUC.
    STOP RUN.

Wrong

01 WS-SONUC PIC 9(3) VALUE ZERO.
PROCEDURE DIVISION.
    COMPUTE WS-SONUC = 500 * 4.
    DISPLAY WS-SONUC.
    STOP RUN.

The result is four digits, but the field is three digits; the leftmost digit is silently discarded and a completely wrong number appears on the screen. To catch overflow, a size error clause must be added or the field must be expanded.

Correct

01 WS-SONUC PIC 9(5) VALUE ZERO.
PROCEDURE DIVISION.
    COMPUTE WS-SONUC = 500 * 4
        ON SIZE ERROR DISPLAY 'Overflow'
    END-COMPUTE.
    DISPLAY WS-SONUC.
    STOP RUN.

Wrong

01 WS-A PIC 9(3) VALUE 10.
01 WS-B PIC 9(3) VALUE 3.
01 WS-SONUC PIC 9(3) VALUE ZERO.
PROCEDURE DIVISION.
    DIVIDE WS-A INTO WS-B GIVING WS-SONUC.
    DISPLAY WS-SONUC.
    STOP RUN.

The divide verb has two prepositions and they reverse the divisor and dividend: this syntax divides the second value by the first, meaning three divided by ten is calculated instead of ten divided by three, and the result in an integer field comes out as zero. Mixing up the preposition silently reverses the operation; the expression form eliminates this ambiguity.

Correct

01 WS-A PIC 9(3) VALUE 10.
01 WS-B PIC 9(3) VALUE 3.
01 WS-SONUC PIC 9(3) VALUE ZERO.
PROCEDURE DIVISION.
    COMPUTE WS-SONUC ROUNDED = WS-A / WS-B.
    DISPLAY WS-SONUC.
    STOP RUN.

Frequently asked questions

Which one should I choose between the verb form and the expression form?

For a single addition or subtraction, the verb form is readable. For formulas involving multiple operations, parentheses, or exponents, the expression form is much clearer and eliminates ambiguity in the order of operations.

What happens to the precision of intermediate results?

In the expression form, the compiler uses as many digits as needed for intermediate results; truncation only occurs at the final assignment. In the verb form, since each step is written to its own target field, precision is lost early in chained calculations.

How do I store negative values?

A sign indicator is added to the beginning of the picture clause. If the indicator is not written, the field is unsigned and the sign is lost when a negative result is assigned; this is a very common mistake in calculations involving subtraction.

Summary

COBOL arithmetic is decimal-based.

Rounding must be explicitly requested.

Narrow result fields truncate silently.

The size error clause catches overflow.

The expression form is safer in formulas.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code