COBOL Example Programs


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.

The programs have been compiled and tested using Microfocus NetExpress but they should work on any COBOL-85 complient compiler with very few changes.
You may have to reformat the programs if your compiler does not have the equivalent of the $ SET SOURCEFORMAT"FREE". This compiler directive frees Microfocus COBOL programs from the normal COBOL formatting conventions.
You may also have to remove the
ORGANIZATION IS LINE SEQUENTIAL phrase in the SELECT and ASSIGN clause of sequential files. In Microfocus COBOL, LINE SEQUENTIAL files terminate each record with a carriage return and line feed.

Copyright Notice

These programs are the copyright property of Michael Coughlan. You have permission to use these programs for your own personal use but you may not reproduce them in any published work without written permission from the author.


Beginners programs

Program Name

Description

Major constructs

Action

Shortest.cbl
This example program is almost the shortest COBOL program we can have. We could make it shorter still by leaving out the STOP RUN.
DISPLAY
Download
View

Accept.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
Download
View
Multiplier.cbl
Accepts two single digit numbers from the user, multiplies them together and displays the result.
ACCEPT, DISPLAY, MULTIPLY
Download
View

 

Selection and Interation

Program Name

Description

Major constructs

Action

IterIf.cbl

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

IF, PERFORM..TIMES, ADD, MULTIPLY
Download
View
Conditions.cbl
An example program demonstrating the use of Condition Names (level 88's).
 
Condition Names (level 88's), EVALUATE, PERFORM..UNTIL
Download
View
Perform1.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
Download
View
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
Download
View
Perform3.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..UNTI, ADD, COMPUTE, ON SIZE ERROR, INITIALIZE
Download
View
Perform4.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
MileageCount.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
Download
View

 

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
 
Download
View
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
Download
View
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
Download
View
SeqInsert.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
Download
View
SeqRpt.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.
Download
View

 

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
Download
View
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
Download
View
Merge.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
Download
View
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
Download
View
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
Download
View
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)
Download
View
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)
Download
View

 

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
Download
View
ReadRel.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
Download
View
Seq2Index.cbl
Creates a direct access Indexed file from a Sequential file. Note that Microfocus 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
Download
View
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.
Download
View
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
Download
View
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
Download
View
Aroma96.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,
Download
View
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
Download
View
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
Download
View
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
Download
View

 

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
Download
View
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
Download
View
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
Download
View
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
Download
View
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
Download
View
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
 
Download
View
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
 
Download
View
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
Download
View
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
Download
View

 

String handling

Program Name

Description

Major constructs

Action

RefMod.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
Download
View
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
Download
View
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
Download
View

 

The COBOL Report Writer

Program Name

Description

Major constructs

Action

RepWriteA.cbl
A simplified version of RepWriteFull.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
Download
View
RepWriteB.cbl
A simplified version of RepWriteFull.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
Download
View
RepWriteFull.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
Download
View
RepWriteSumm.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
Download
View

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

Download
View

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
Download
View
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
Download
View

 


Back to Student Resources To Student Resources Please fill out our evaluation form Evaluation Form Search the CSIS Web Site Search CSIS Web Site
Selectable Site Contents


Last updated : June 2002
e-mail : CSISwebeditor@ul.ie