These pages contain a large number of example COBOL programs. The programs may be downloaded, or viewed in your browser. Test data for the example COBOL programs is also provided where appropriate.


Console I/O
Program Name Description Major constructs Action
HelloWorld.cbl
A minimal COBOL program that displays "Hello World". About as short as a working COBOL program can get — you could trim it further by leaving out the STOP RUN.
DISPLAY

AcceptAndDisplay.cbl

The program accepts a simple student record from the user and displays the individual fields. Also shows how the ACCEPT may be used to get and DISPLAY the system time and date.
ACCEPT, DISPLAY, ACCEPT FROM DATE, ACCEPT FROM DAY, ACCEPT FROM TIME
Multiplier.cbl
Accepts two single digit numbers from the user, multiplies them together and displays the result.
ACCEPT, DISPLAY, MULTIPLY
Selection and Iteration
Program Name Description Major constructs Action
Iteration-If.cbl

An example program that implements a primitive calculator. The calculator only does additions and multiplications.

IF, PERFORM..TIMES, ADD, MULTIPLY
Conditions.cbl
An example program demonstrating the use of Condition Names (level 88's).
Condition Names (level 88's), EVALUATE, PERFORM..UNTIL
PerformFormat1.cbl
An example program demonstrating how the first format of the PERFORM may be used to change the flow of control through a program.
PERFORM
Perform2.cbl
Demonstrates the second format of the PERFORM. The PERFORM..TIMES may be used to execute a block of code x number of times.
PERFORM..TIMES
PerformFormat3.cbl
Demonstrates how the PERFORM..UNTIL (third format) may be used to process a stream of values where the length of the stream cannot be determined in advance (although in this case there is a set maximum number of values in the stream).
PERFORM..UNTIL, ADD, COMPUTE, ON SIZE ERROR, INITIALIZE
PerformFormat4.cbl
Demonstrates how the PERFORM..VARYING and the PERFORM..VARYING..AFTER (fourth format) may be used for counting iteration. Also introduces the WITH TEST BEFORE and WITH TEST AFTER phrases.
PERFORM..VARYING..AFTER, DISPLAY
Download
View
MileageCounter.cbl
Demonstrates how the PERFORM..VARYING and the PERFORM..VARYING..AFTER (fourth format) may be used to simulate a mileage counter.
PERFORM..VARYING..AFTER, DISPLAY
Sequential Files
Program Name Description Major constructs Action
SeqWrite.cbl
Example program demonstrating how to create a sequential file from data input by the user.
Sequential Files, WRITE, OPEN, CLOSE, File Description (FD), SELECT..ASSIGN
SeqReadno88.cbl
An example program that reads a sequential file and displays the records. This version does not use level 88's to signal when the end of the file has been reached.
To test the program download thistestdata file.
Sequential Files, READ, OPEN, CLOSE, File Description (FD), SELECT..ASSIGN
SeqRead.cbl
An example program that reads a sequential file and displays the records. This is the correct version which uses the Condition Name (level 88) "EndOfFile"to signal when the end of the file has been reached.
To test the program download this testdata file.
Sequential Files, Condition Names (level 88's) READ, OPEN, CLOSE, File Description (FD), SELECT..ASSIGN
InsertRecords.cbl
Demonstrates how to insert records into a sequential file from a file of transaction records. A new file is created which contains the inserted records.
To test this program you will need to download Student Masterfile - Students.dat and the Transaction file Transins.dat .
Sequential Files, Condition Names (level 88's) READ, WRITE
StudentNumbersReport.cbl
This program reads records from the student file, counts the total number of student records in the file and the number records for females and males. Prints the results in a very short report.
To test the program download Students.dat
Sequential Files, Condition Names (level 88's) READ, WRITE, Writing to a report file.
SeqUpdate.cbl
Updates the Students.dat master file using course-transfer transactions from Transfer.dat to produce a new Students.New file. Demonstrates the sequential file matching algorithm and error detection for unmatched or mismatched transaction records.
To test this program you will need to download Student Masterfile - Students.dat and the Transaction file Transfer.dat.
Sequential Files, Condition Names (level 88's), READ, WRITE, EVALUATE
Sorting and Merging
Program Name Description Major constructs Action
InputSort.cbl
Uses the SORT and an INPUT PROCEDURE to accept records from the user and sort them on ascending StudentId.
SORT, Input Procedure
MaleSort.cbl
Sorts the student masterfile (sorted on ascending Student Id) and produces a new file, sorted on ascending student name, containing only the records of male students.
To test the program download Students.dat
SORT, Input Procedure
MergeFiles.cbl
Uses the MERGE to insert records from a transaction file into a sequential master file. .
To test this program you will need to download Student Masterfile - Students.dat and the Transaction file Transins.dat .
MERGE
AromaSalesRpt.cbl
Exam paper model answer. A program is required to produce a summary sales report from an unsorted sequential file containing the details of sales of essential and base oils to Aromamora customers.
Read the program specification first. The required data files may be downloaded from there.
SORT with Input Procedure and Output Procedure, Print Files, Sequential Files, COMPUTE
CSISEmailDomain.cbl

Exam paper model answer. A program is required which will produce a file, sorted on ascending email domain, from the unsorted GraduateInfo file.
Read the program specification first. The required data files may be downloaded from there.

SORT with Input Procedure and Output Procedure, Sequential Files, Pre-Defined Tables, Run time filled tables, SEARCH
BestSellers.cbl
Exam paper model answer. The Folio Society is a Book Club that sells fine books to customers all over the world. A program is required to print a list of the ten best selling books (by copies sold) in the Book Club.
Read the program specification first. The required data files may be downloaded from there.
(Sequential Files, Print Files, SORT with Input Procedure and Output Procedure, Tables)
DueSubRpt.cbl
Exam paper model answer. NetNews is a company which runs an NNTP server carrying the complete USENET news feed with over 15,000 news groups. Access to this server is available to internet users all over the world. Users of the system pay a subscription fee for access. A program is required to produce a report showing the subscriptions which are now due. The report will be based on the Due Subscriptions file. (Sequential Files, Print Files, SORT with Input Procedure, Tables, SEARCH ALL)
Direct Access Files
Program Name Description Major constructs Action
Seq2Rel.cbl
Creates a direct access Relative file from a Sequential File. Note that Relative files are not standard ASCII files and so cannot be created with an editor as Sequential files can.
To create the Relative file you will need to download the supplier file SEQSUPP.dat
Relative files, Sequential Files, ACCESS MODE, RELATIVE KEY, FILE STATUS, READ..AT END, WRITE..INVALID KEY
ReadRelative.cbl
Reads the Relative file - RELSUPP.dat - created in the previous example program and displays the records. Allows the user to choose to read through and display all the records in the file sequentially, or to use a key to read just one record directly.
Relative files, ACCESS MODE, RELATIVE KEY, FILE STATUS, READ..NEXT RECORD, READ..INVALID KEY, Condition Names, IF
Seq2Index.cbl
Creates a direct access Indexed file from a Sequential file. Note that GnuCOBOL Indexed files actually consist of two files - the data file and the index file (.idx).
To create the Indexed file you will need to download the supplier file SEQVIDEO.dat
Indexed files, Sequential Files, RECORD KEY, ALTERNATE KEY, READ..AT END, WRITE..INVALID KEY
DirectReadIdx.cbl
Does a direct read on the Indexed file created by the previous example program. Allows the user to choose which of the keys to use for the direct read.
Indexed files, READ..KEY IS.
SeqReadIdx.cbl
Reads the Indexed file sequentially on whichever key is chosen by the user. Displays all the records in the file.
Indexed files, READ..NEXT RECORD, START
StudFees.cbl

Exam paper model answer. A program which applies a transaction file of student payments to a Student Master File and which then produces a report showing those students whose fees are partially or wholly outstanding. Read the program specification first. The required data files may be downloaded from there.

Indexed files, Print files, READ..NEXT RECORD, START, WRITE, REWRITE, IF, PERFORM, ADD, SUBTRACT
Aroma96exam.cbl
Exam paper model answer. A program is required which will apply a file of sorted, validated transactions to the Oil-Stock-File and then produce a report based on the contents of both the Oil-Details-File and the Oil-Stock-File. The report writer must be used to produce the report which must be printed sequenced on ascending Oil-Name.
Read the program specification first. The required data files may be downloaded from there.
Indexed files, Relative Files, Sequential Files, EVALUATE, COMPUTE, READ, WRITE, REWRITE, START, Report Writer, Report Section, INITIATE, GENERATE, TERMINATE,
BookshopRpt91.cbl

Exam paper model answer. A program is required to produce a Purchase Requirements Report from the Publisher, Book and Purchase Requirements files. The report should be sequenced on ascending Publisher Name and should only detail the purchase requirements for the semester under scrutiny.
Read the program specification first. The required data files may be downloaded from there.

Indexed files, Report Writer, Report Section, INITIATE, GENERATE, TERMINATE, READ, WRITE, REWRITE, START
LibRoyaltyRpt.cbl
Exam paper model answer. Each time a book is borrowed, the Pascal Memorial Library pays the author a small sum of money as royalty. Royalties are paid to authors through their agents. A report is required which processes the Authors file and the Books File to produce a report which shows the amount to be sent to each agent, shows the breakdown of the agent payment into author payments, and shows the breakdown of each author payment into royalty payments per book.
Read the program specification first. The required data files may be downloaded from there.
Indexed Files, Print Files, READ..NEXT RECORD, READ..KEY IS, START, REWRITE, WRITE, MULTIPLY, SET
TopSuppliersRpt.cbl Exam paper model answer. The manager of Metropolis Videos has asked you to write a program to produce a report showing the top three Video Suppliers (by total store video earnings) and for each of the top three the most popular video title (by average earnings) must be shown.
Read the program specification first. The required data files may be downloaded from there.
Indexed Files, Relative Files, Print Files, READ, START, Tables, DIVIDE
CALLing sub-programs
Program Name Description Major constructs Action
DriverProg.cbl
This program demonstrates the CALL verb by calling three external sub-programs.
The "MultiplyNums" sub-program demonstrates flow of control - from caller to called and back again - and the use of parameters.
The "Fickle" sub-program demonstrates State Memory.
The "Steadfast" sub-program shows how a sub-program can use the IS INITIAL phrase to avoid State Memory.
Calling external sub-programs. CALL..BY REFERENCE, CALL..BY CONTENT, COMP, CANCEL
MultiplyNums.cbl
The "MultiplyNums" sub-program, called from the driver program above, demonstrates the flow of control from the driver program to the called sub-program and back again. It uses numeric and string parameters and demonstrates the BY REFERENCE (the default) and BY CONTENT parameter passing mechanisms.
Calling external sub-programs, LINKAGE SECTION, EXIT PROGRAM
Fickle.cbl
Fickle is a sub-program called from DriverProg.cbl above. Fickle is a program that demonstrates State Memory. Each time the program is called it remembers its state from the previous call.
Calling external sub-programs, LINKAGE SECTION, EXIT PROGRAM
Steadfast.cbl
Steadfast is a sub-program called from DriverProg.cbl above. Steadfast is identical to Fickle except that it uses the IS INITIAL phrase to avoid State Memory.
Calling external sub-programs, IS INITIAL, LINKAGE SECTION, EXIT PROGRAM
DateDriver.cbl
A driver program for the date validation sub-program below. Accepts a date from the user, passes it to the date validation sub-program and interprets and displays the result.
External sub-programs, CALL, ACCEPT, DISPLAY, EVALUATE
Validate.cbl
A date validation sub-program. Takes a date parameter in the form DDMMYYYY and returns a code indicating if the date was valid or not. If not valid, the code indicates why the date was not valid (e.g. day to great for month).
External sub-programs, IS INITIAL, LINKAGE SECTION, EVALUATE, IF..ELSE, DIVIDE..REMAINDER, SET ConditionName, EXIT PROGRAM
DayDiffDriver.cbl
A driver program that accepts two dates from the user and displays the difference in days between them. This program uses the Validate.cbl sub-program above and also contains a number of contained sub-programs.
CALL, LINKAGE SECTION, Contained Sub-Programs, External Sub-Programs, Intrinsic Functions, EXIT PROGRAM, END PROGRAM, FUNCTION INTEGER-OF-DATE
ACME99.cbl

Exam paper model answer. A program is required which will process the Stock and Manufacturer files to create an Orders file containing ordering information for all the parts that need to be re-ordered (i.e. where the QtyInStock is less than the ReorderLevel). For EU countries only, the COSTOFITEMS and POSTAGE fields of each Orders file record must be calculated. The Postage rate is obtained by calling an existing program.
Read the program specification first. The required data files may be downloaded from there.

Indexed files, Relative Files, SubPrograms, CALL..USING, READ, WRITE, REWRITE, Condition Names (level 88s), EVALUATE, UNSTRING, MULTIPLY..ON SIZE ERROR
SFbyMail.cbl
Exam paper model answer. A program is required to apply the Orders file (containing customer orders) to the indexed Book Stock file and to produce the Processed Orders file.
Read the program specification first. The required data files may be downloaded from there.
CALL, subprograms, Sequential files, Indexed files, READ, WRITE, REWRITE, COMPUTE, UNSTRING
String handling
Program Name Description Major constructs Action
RefModification.cbl
Solves a number of string handling tasks such as : - Extracting a substring from a string given the start position and length of the substring.
Extracting the first x number of chars from a string.
Extracting the last x number of chars from a string.
Removing trailing blanks from a string.
Removing leading blanks from a string.
Finding the location of the first occurrence of any of a substring's chars in a string
Reference Modification, INSPECT, Intrinsic Functions, FUNCTION REVERSE
UnstringFileEg.cbl
An example showing the unpacking of comma separated records and the size validation of the unpacked fields.
To test this program use the data file VarLen.dat.
UNSTRING, Reference Modification, IF, Condition Names, Sequential files
FileConv.cbl
Exam paper model answer. A program is required to convert the unsorted Client-Names file of free-format, variable length, records into a sorted file of fixed length records. The sorted file must be sorted into ascending Client-Name within ascending County-Name. In addition to converting and sorting the file, the program must ensure that the county name is valid and must convert it to a county number.
Read the program specification first. The required data files may be downloaded from there.
Sequential files, SORT with Input Procedure, Pre-Defined Table of values, STRING, UNSTRING, INSPECT, SEARCH ALL
The COBOL Report Writer
Program Name Description Major constructs Action
ReportExampleA.cbl
A simplified version of ReportExampleFull.cbl using only one control break.
All four of these Report Writer programs use the GBsales.dat data file.
Report Writer, Report Section, INITIATE, GENERATE, TERMINATE
ReportExampleB.cbl
A simplified version of ReportExampleFull.cbl containing all the control breaks but not using Declaratives.
All four of these Report Writer programs use the GBsales.dat data file.
Report Writer, Report Section, INITIATE, GENERATE, TERMINATE
ReportExampleFull.cbl
The full version of the report program containing all the control breaks and using Declaratives to calculate the salesperson salary and commission.
All four of these Report Writer programs use the GBsales.dat data file.
Report Writer, Report Section, INITIATE, GENERATE, TERMINATE, Declaratives, USE BEFORE REPORTING
ReportExampleSummary.cbl
The summary version of the full report program containing all the control breaks and using Declaratives to calculate the salesperson salary and commission.
All four of these Report Writer programs use the GBsales.dat data file.
Report Writer, Report Section, INITIATE, GENERATE, TERMINATE, Declaratives, USE BEFORE REPORTING
COBOL Tables
Program Name Description Major constructs Action
MonthTable.cbl

This program counts the number of students born in each month and displays the result.
The program uses a pre-filled table of month names.
This program is the solution to one of the programming exercises.
Read the program specification first.

Pre-Filled Tables, Sequential files, PERFORM..VARYING, PERFORM..UNTIL
FlyByNight.cbl
Exam paper model answer. A program is required which will read the Booking Master file to produce a Summary file sequenced upon ascending Destination-Name.
Read the program specification first. The required data files may be downloaded from there.
Pre-Filled Tables, Sequential Files, SORT with Input Procedure, SEARCH, INSPECT
USSRship.cbl
Exam paper model answer. A program is required to produce a report detailing the major vessels (Vessels of tonnage 3,500 or greater and all submarines) in each of oceans and major seas of the world.
Read the program specification first. The required data files may be downloaded from there.
Sequential files, SORT with Input Procedure, Print Files, Pre-defined tables, Condition Names