top of page

Show me the source code to build a basic Cobol module along with an output

IDENTIFICATION DIVISION.
       PROGRAM-ID. HelloWorld.

       ENVIRONMENT DIVISION.

       DATA DIVISION.

       WORKING-STORAGE SECTION.
       01  WS-GREETING     PIC A(20) VALUE 'Hello, World!'.

       PROCEDURE DIVISION.

       DISPLAY WS-GREETING.

       STOP RUN.

Sample Output:

Hello, World!

 

How to Compile the COBOL Program​:

cobc -x HelloWorld.cob
 

How to run the COBOL Program​

./HelloWorld

Show me the source code with a table of data I can use to run a basic cobol program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TableExample.

       ENVIRONMENT DIVISION.

       DATA DIVISION.

       WORKING-STORAGE SECTION.
       01  WS-TABLE-SIZE       PIC 9(02) VALUE 5.
       01  WS-INDEX            PIC 9(02) VALUE 1.

       01  WS-NAME-TABLE.
           05  WS-NAME OCCURS 5 TIMES INDEXED BY IDX.
               10  WS-FIRST-NAME  PIC A(20).
               10  WS-LAST-NAME   PIC A(20).

       PROCEDURE DIVISION.

       * Initialize table data
       MOVE 'John'        TO WS-FIRST-NAME (1).
       MOVE 'Doe'         TO WS-LAST-NAME (1).
       MOVE 'Jane'        TO WS-FIRST-NAME (2).
       MOVE 'Smith'      TO WS-LAST-NAME (2).
       MOVE 'Alice'        TO WS-FIRST-NAME (3).
       MOVE 'Johnson'  TO WS-LAST-NAME (3).
       MOVE 'Bob'          TO WS-FIRST-NAME (4).
       MOVE 'Brown'     TO WS-LAST-NAME (4).
       MOVE 'Charlie'    TO WS-FIRST-NAME (5).
       MOVE 'Davis'       TO WS-LAST-NAME (5).

       * Display header
       DISPLAY 'Name List:'.

       * Loop through the table and display names
       PERFORM DISPLAY-NAMES UNTIL WS-INDEX > WS-TABLE-SIZE.

       STOP RUN.

       DISPLAY-NAMES.
           DISPLAY 'First Name: ' WS-FIRST-NAME (WS-INDEX)
                   '  Last Name: ' WS-LAST-NAME (WS-INDEX).
           ADD 1 TO WS-INDEX.

 

Sample Output:

​

Name List:
First Name: John  Last Name: Doe
First Name: Jane  Last Name: Smith
First Name: Alice  Last Name: Johnson
First Name: Bob  Last Name: Brown
First Name: Charlie  Last Name: Davis

bottom of page