Skip to main content
CodeSpeedTest
Languages
Start TypingJump into a test — pick any languageAdaptive TrainingUnlock chars as you master themPractice DrillsFocused sessions targeting weak spotsDaily ChallengesNew coding challenges every dayRace ModeCompete against others in real timeAI OpponentRace against an AI at your WPM levelTournamentsLive coding speed tournamentsArcade GamesZType, Overkill Survival, Glyphica & moreGamificationXP, coins, badges & quests
LeaderboardGlobal rankings for every languageCertificatesEarn verifiable Bronze / Silver / Gold certsActivityDaily streaks & historical analyticsProfileYour stats, badges & achievements
Browse Languages500+ languages with real code examplesBlogTips, guides & deep divesFree ToolsWPM calculator, typing speed report & moreFAQCommon questions answeredGetting StartedNew to CodeSpeedTest?AboutOur story & missionSupportGet help — Pro users get priorityContactGet in touch with the team
Pricing
  1. Home
  2. /
  3. Learn
  4. /
  5. Cobol

Learn Cobol - 9 Code Examples & CST Typing Practice Test

COBOL (Common Business-Oriented Language) is a high-level, English-like programming language designed for business applications, financial systems, batch processing, and large-scale enterprise data operations. Known for reliability, readability, and long-term stability.

View all 9 Cobol code examples →
COBOL Counter and Theme ToggleCOBOL Simple AdditionCOBOL FactorialCOBOL Fibonacci SequenceCOBOL Array SumCOBOL Max of Two NumbersCOBOL Reverse StringCOBOL Even Numbers FilterCOBOL String Concatenation

Learn COBOL with Real Code Examples

Updated Nov 19, 2025

Explain

COBOL is optimized for business data processing, not system-level or performance-critical tasks.

It features a verbose, English-like syntax designed for readability by non-programmers in the 1960s.

Used extensively in banking, insurance, government, and enterprise legacy systems for decades.

Core Features

DIVISION-based program structure

Built-in file handling for sequential and indexed data

Extensive record definitions via DATA DIVISION

Procedural logic through PROCEDURE DIVISION

Batch processing and transaction support

Basic Concepts Overview

Divisions and sections

Variables and data types via PIC clauses

Records and file structures

Procedural flow with PERFORM statements

Batch input/output and file operations

Project Structure

source.cob

copybooks (.cpy)

JCL scripts for batch execution (mainframe)

Compiled binary or mainframe load module

Dataset definitions

Building Workflow

Write .cob or .cbl program

Compile using cobc or mainframe compiler

Link libraries (if required)

Run executable locally or submit batch job

Validate results through dataset outputs

Difficulty Use Cases

Beginner: simple calculations or text output

Intermediate: file processing and record manipulation

Advanced: batch systems with multiple datasets

Expert: full financial transaction systems or mainframe integration

Comparisons

More verbose than modern languages

Far more stable for business logic

Better suited for batch than real-time apps

Strong legacy support vs modern innovation

More maintainable for long-term enterprise systems

Versioning Timeline

1959 - COBOL 60

1974 - COBOL-74 (major standardization)

1985 - COBOL-85 (structured programming)

2002 - Object-oriented COBOL

2014-2025 - Modern compiler support & cloud COBOL

Glossary

Copybook: reusable data layout file

Division: major program section

Record: structured data unit

Dataset: mainframe data storage

JCL: Job Control Language for batch jobs

Installation Setup

Install OpenCOBOL/GNUCOBOL compiler

Optionally install IBM Enterprise COBOL (licensed)

Set up environment variables for cobc compiler

Write initial COBOL hello world program

Compile via cobc -x file.cob

Environment Setup

Install COBOL compiler

Configure runtime environment

Set PATH for cobc or MF tools

Prepare datasets or input files

Run test batch jobs

Config Files

Copybooks (.cpy)

JCL job scripts

DB2 configuration files

Micro Focus project configs

Dataset definitions

Cli Commands

cobc -x program.cob

cobc -c program.cob

submit JCL job

db2 bind/run

microfocus CLI operations

Internationalization

Supports international character sets

Handles multi-format numeric and text data

Compatible with mainframe localization standards

Record formats accommodate global specs

Used globally in banking/government

Accessibility

Readable English syntax

Clear program structure

Verbose format aids understanding

Large enterprise documentation

Strong community around mainframes

Ui Styling

COBOL is not a UI-first language

Simple text screens for input/output

CICS screens for transactions

Terminal-based UI layouts

No built-in graphical UI capabilities

State Management

Variables defined via PIC clauses

Records maintain structured data

Working-Storage Section for persistent state

Local state via PERFORM routines

Files store external state

Data Management

Sequential & indexed file handling

Record definitions in DATA DIVISION

Copybooks for consistent layouts

DB2 or VSAM dataset integration

Batch input/output streams

Architecture

Programs divided into IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE divisions

Strong emphasis on record-based data structures

Runs primarily on mainframes (IBM z/OS) or COBOL compilers on modern OSes

Sequential and indexed file models

Batch execution via JCL workflows on mainframes

Rendering Model

COBOL source code

Compiled into machine code or bytecode

Executed on mainframes or local OS

Batch jobs handle large datasets

Screen/terminal I/O via DISPLAY/ACCEPT

Architectural Patterns

Batch-driven processing

Modular sections and paragraphs

Copybook-based data reuse

Record-oriented data handling

Mainframe transaction workflows (CICS)

Real World Architectures

Core banking systems

Insurance claim engines

Government benefit systems

Payment processing pipelines

Enterprise mainframe workloads

Design Principles

Self-documenting English-like syntax

Structured business-oriented logic

Strong emphasis on data integrity

Batch-oriented workflows

Long-term maintainability

Scalability Guide

Run batch jobs in parallel partitions

Optimize file access patterns

Modularize business logic

Leverage mainframe parallelism

Use Load modules efficiently

Migration Guide

Refactor COBOL to microservices via wrappers

Convert datasets to relational DBs

Rewrite modules in modern languages if feasible

Preserve business logic during modernization

Migrate batch jobs to cloud schedulers

Performance Notes

Use efficient file access modes (indexed vs sequential)

Minimize nested PERFORM loops

Use batch processing optimally for large data

Leverage compiler optimization flags

Limit unnecessary screen/file I/O

Security Notes

Protect sensitive financial data in records

Ensure proper access control in mainframe environments

Validate data formats for legacy systems

Encrypt datasets where applicable

Avoid exposing mainframe credentials in JCL

Monitoring Analytics

Log business events via datasets

Use DISPLAY for debug tracing

Analyze batch job reports

Profile runtime on mainframe monitors

Track dataset growth over time

Code Quality

Use clear section/paragraph names

Document data layouts extensively

Keep logic modular

Adopt structured programming style

Validate record formats often

Practical Examples

Payroll calculation program

Banking transaction batch processor

Insurance policy renewal logic

Reading/writing indexed files

Generating enterprise reports

Troubleshooting

Check PIC clause alignments

Verify file organization and access mode

Debug logic flow with DISPLAY statements

Ensure correct mainframe dataset definitions

Validate numeric precision in computations

Testing Guide

Unit test individual paragraphs

Mock file inputs using test datasets

Validate record layouts thoroughly

Run batch jobs in test LPARs

Perform regression testing for large systems

Deployment Options

Mainframe job submission (JCL)

Executable binaries for Linux/Windows via GnuCOBOL

Integration within larger enterprise pipelines

COBOL web services via Micro Focus

Cloud-hosted COBOL runtimes

Tools Ecosystem

GnuCOBOL (open source)

IBM Enterprise COBOL

Micro Focus Visual COBOL

COBOL copybook tools

Mainframe debuggers

Integrations

Mainframe systems (z/OS, CICS, JCL)

Database systems (DB2)

MQ messaging

COBOL-C/C++ bridges

Modern cloud APIs via wrappers

Productivity Tips

Use copybooks for reuse

Structure programs into clear sections

Leverage mainframe debugging tools

Validate data formats early

Comment complex business logic extensively

Challenges

Write a payroll summary program

Read and process large sequential files

Build a mini banking ledger

Create report generation script

Integrate COBOL with DB2

Learning Path

Start with basic COBOL syntax and divisions

Learn PIC data types and file handling

Practice sequential & indexed file operations

Study batch processing with JCL

Explore enterprise-level systems and DB2 integration

Skill Improvement Plan

Week 1: Syntax & divisions

Week 2: File input/output

Week 3: Batch processing & JCL

Week 4: Complex business logic & DB2

Interview Questions

Explain COBOL's four divisions.

What is the PIC clause used for?

Difference between sequential and indexed files?

How does PERFORM differ from CALL?

How do you handle batch input/output?

Cheat Sheet

IDENTIFICATION DIVISION.

DATA DIVISION.

PROCEDURE DIVISION.

PERFORM UNTIL condition ... END-PERFORM.

MOVE value TO variable.

Books

Murach's Mainframe COBOL

COBOL for the 21st Century

Teach Yourself COBOL in 21 Days

Tutorials

COBOL programming for beginners

Mainframe COBOL & JCL tutorials

COBOL file processing guides

Official Docs

ISO COBOL standards

IBM Enterprise COBOL documentation

GnuCOBOL user manual

Community Links

COBOL/Mainframe Reddit communities

IBM Developer forums

StackOverflow COBOL tag

Community Support

COBOL programming communities

Mainframe developer groups

Reddit r/mainframe

StackOverflow COBOL tag

IBM Developer forums

Monetization

COBOL modernization consulting

Mainframe development roles

Financial/enterprise contract work

Government system maintenance

COBOL-cloud integration services

Future Roadmap

Continued relevance in legacy modernization

More cloud-enabled COBOL runtimes

Growing demand for COBOL developers

Better tooling for modernization

Long-term coexistence with modern tech

When Not To Use

Web or mobile app development

High-performance graphics or gaming

Real-time systems programming

Startups needing rapid iteration

Projects requiring modern paradigms

Final Summary

COBOL remains essential for enterprise data processing.

Highly stable and readable for long-term business applications.

Integral to banking, government, and financial operations.

New developers are needed to maintain aging legacy systems.

Faq

Is COBOL still used?

Yes - it powers a massive amount of global financial and government infrastructure.

Is COBOL easy to learn?

Syntax is simple but verbose.

Does COBOL run on modern systems?

Yes - through GnuCOBOL and enterprise tools.

Is COBOL only on mainframes?

Primarily, but it can run on Windows/Linux via compilers.

Code Sample Descriptions

1

COBOL Counter and Theme Toggle

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CounterProgram.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 COUNT        PIC 9(4) VALUE 0.
       01 IS-DARK      PIC X VALUE 'N'.

       PROCEDURE DIVISION.
       DISPLAY 'Counter: ' COUNT
       DISPLAY 'Theme: ' IS-DARK

       ADD 1 TO COUNT
       DISPLAY 'Counter: ' COUNT

       MOVE 'Y' TO IS-DARK
       DISPLAY 'Theme: ' IS-DARK

       SUBTRACT 1 FROM COUNT
       DISPLAY 'Counter: ' COUNT

       MOVE 0 TO COUNT
       DISPLAY 'Counter: ' COUNT

       STOP RUN.

Demonstrates a simple counter with theme toggling using COBOL variables and console output.

Let’s Try →
2

COBOL Simple Addition

       IDENTIFICATION DIVISION.
       PROGRAM-ID. AddProgram.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 A   PIC 9(4) VALUE 5.
       01 B   PIC 9(4) VALUE 3.
       01 SUM PIC 9(4) VALUE 0.

       PROCEDURE DIVISION.
       ADD A TO B GIVING SUM
       DISPLAY 'Sum: ' SUM
       STOP RUN.

Adds two numbers and displays the result.

Let’s Try →
3

COBOL Factorial

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FactorialProgram.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N       PIC 9(2) VALUE 5.
       01 FACT    PIC 9(5) VALUE 1.
       01 COUNTER PIC 9(2) VALUE 1.

       PROCEDURE DIVISION.
       PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > N
           MULTIPLY FACT BY COUNTER GIVING FACT
       END-PERFORM
       DISPLAY 'Factorial: ' FACT
       STOP RUN.

Calculates factorial of 5 using a loop.

Let’s Try →
4

COBOL Fibonacci Sequence

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FibonacciProgram.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N      PIC 9(2) VALUE 10.
       01 A      PIC 9(5) VALUE 0.
       01 B      PIC 9(5) VALUE 1.
       01 TEMP   PIC 9(5) VALUE 0.
       01 COUNTER PIC 9(2) VALUE 1.

       PROCEDURE DIVISION.
       DISPLAY A
       PERFORM VARYING COUNTER FROM 2 BY 1 UNTIL COUNTER > N
           ADD A TO B GIVING TEMP
           DISPLAY B
           MOVE B TO A
           MOVE TEMP TO B
       END-PERFORM
       STOP RUN.

Generates first 10 Fibonacci numbers.

Let’s Try →
5

COBOL Array Sum

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ArraySum.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUMBERS.
          05 NUM PIC 9(2) OCCURS 5 TIMES VALUE 1 2 3 4 5.
       01 SUM PIC 9(3) VALUE 0.
       01 INDEX PIC 9(2) VALUE 1.

       PROCEDURE DIVISION.
       PERFORM VARYING INDEX FROM 1 BY 1 UNTIL INDEX > 5
           ADD NUM (INDEX) TO SUM
       END-PERFORM
       DISPLAY 'Sum: ' SUM
       STOP RUN.

Sum elements of an array.

Let’s Try →
6

COBOL Max of Two Numbers

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MaxProgram.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 A   PIC 9(4) VALUE 7.
       01 B   PIC 9(4) VALUE 10.
       01 MAX PIC 9(4) VALUE 0.

       PROCEDURE DIVISION.
       IF A > B THEN
           MOVE A TO MAX
       ELSE
           MOVE B TO MAX
       END-IF
       DISPLAY 'Max: ' MAX
       STOP RUN.

Finds the maximum of two numbers.

Let’s Try →
7

COBOL Reverse String

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ReverseString.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STR PIC X(10) VALUE 'HELLO'.
       01 REV PIC X(10).
       01 INDEX1 PIC 9(2) VALUE 5.
       01 INDEX2 PIC 9(2) VALUE 1.

       PROCEDURE DIVISION.
       PERFORM VARYING INDEX2 FROM 1 BY 1 UNTIL INDEX2 > 5
           MOVE STR (INDEX1:1) TO REV (INDEX2:1)
           SUBTRACT 1 FROM INDEX1
       END-PERFORM
       DISPLAY 'Reversed: ' REV
       STOP RUN.

Reverses a string and displays it.

Let’s Try →
8

COBOL Even Numbers Filter

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EvenNumbers.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUMS.
          05 NUM PIC 9(2) OCCURS 10 TIMES VALUE 1 2 3 4 5 6 7 8 9 10.
       01 INDEX PIC 9(2) VALUE 1.

       PROCEDURE DIVISION.
       PERFORM VARYING INDEX FROM 1 BY 1 UNTIL INDEX > 10
           IF NUM (INDEX) MOD 2 = 0 THEN
               DISPLAY NUM (INDEX)
           END-IF
       END-PERFORM
       STOP RUN.

Displays even numbers from an array.

Let’s Try →
9

COBOL String Concatenation

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ConcatStrings.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STR1 PIC X(5) VALUE 'HELLO'.
       01 STR2 PIC X(5) VALUE 'WORLD'.
       01 RESULT PIC X(10).

       PROCEDURE DIVISION.
       STRING STR1 DELIMITED BY SPACE
              STR2 DELIMITED BY SPACE
              INTO RESULT
       END-STRING
       DISPLAY 'Concatenated: ' RESULT
       STOP RUN.

Concatenates two strings and displays result.

Let’s Try →

Frequently Asked Questions about Cobol

What is Cobol?

COBOL (Common Business-Oriented Language) is a high-level, English-like programming language designed for business applications, financial systems, batch processing, and large-scale enterprise data operations. Known for reliability, readability, and long-term stability.

What are the primary use cases for Cobol?

Banking and financial transaction systems. Payroll and HR systems. Insurance policy and claims processing. Government mainframe workloads. Batch data processing. Large-scale enterprise reporting

What are the strengths of Cobol?

Highly readable and self-documenting. Rock-solid stability for mission-critical systems. Excellent for large-scale batch processing. Unmatched legacy compatibility. Decades of proven reliability in finance/government

What are the limitations of Cobol?

Verbose syntax compared to modern languages. Not suited for modern web/app development. Small pool of new COBOL developers. Mainframe ecosystem can be expensive. Limited abstraction mechanisms

How can I practice Cobol typing speed?

CodeSpeedTest offers 9+ real Cobol code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.

Learn Other Programming Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlinPartiqlHaskellElixirFsharpView all languages →
CodeSpeedTest

Improve your coding speed, code accuracy, and programming syntax WPM with practice sessions across 500+ programming languages.

Quick Links

HomeAboutFeaturesGetting StartedLanguages

Legal & Support

Pro ⚡ PricingContactPrivacy PolicyTerms of Service

Connect

CodeSpeedTest on GitHubCodeSpeedTest on TwitterEmail CodeSpeedTest

© 2026 CodeSpeedTest. All rights reserved.