Qur'an – Bible.

* Religion * Politics * News Networks * Mainstream Media Biased Reporting * Independent Analysis

Sample COBOL Progam For Students.

Posted by QB on November 19, 2007

Sample COBOL Program for Students.

The following is the sample program for COBOL students. The COBOL students can use this program as a model to complete their assignments. This is also very important to clearly understand the simple logic of the program. This program will print two reports reading Sales input file. The End Of File indicator is initialized to “NO ” in the working storage section. The Page Size define the number of lines printed on each page, line count will be used to print headings. The totals will be printed after processing all records in Sales File. Students have to align the report columns, also may have to change some of the code according to their editor requirement.

IDENTIFICATION DIVISION.
       PROGRAM-ID.                     SALESRPT.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT SALES-FILE ASSIGN TO "SALESEDIT.TXT"
                   ORGANIZATION IS SEQUENTIAL.

            SELECT PRINT-FILE ASSIGN   TO  "PRINTER".

            SELECT ERROR-REPORT ASSIGN TO  "PRINTER".
       DATA DIVISION.
       FILE SECTION.
       FD   SALES-FILE.
       01   SALES-REC.
            05 SP-REC-CODE             PIC X(2).
            05 SP-REGION               PIC X(2).
            05 SP-TERRITORY            PIC 9(4).
            05 SP-NO                   PIC 9(3).
            05 SP-NAME                 PIC X(25).
            05 UNIT-PRICE              PIC 9(2)V99.
            05 UNITS-SOLD              PIC 9(3).

       FD   PRINT-FILE.
       01   PRINT-REC                  PIC X(132).

       FD   ERROR-REPORT.
       01   ERROR-REC               PIC X(132).

       WORKING-STORAGE SECTION.
       77   EOF                        PIC X(3)    VALUE "NO ".
       77   FIRST-REC                  PIC X(1)    VALUE "Y".
       77   SAVE-SP-NO                 PIC 9(3)    VALUE ZEROS.
       77   PAGE-SIZE                  PIC 9(2)    VALUE 60.

       77   LINE-COUNT                 PIC 9(2)    VALUE ZEROS.
       77   LINE-SPACING               PIC 9(2)    VALUE ZEROS.

       01   HEADING-LINE1.
            05 FILLER                  PIC X(132)  VALUE ALL  "_".
       01   HEADING-LINE2.
            05 FILLER                  PIC X(6)    VALUE "REGION".
            05 FILLER                  PIC X(2).
            05 FILLER                  PIC X(13)   VALUE "SALESPERSONS".
            05 FILLER                  PIC X(13)   VALUE "SALESPERSONS".
            05 FILLER                  PIC X(2).
            05 FILLER                  PIC X(13)   VALUE "SALESPERSONS".
            05 FILLER                  PIC X(2).
            05 FILLER                  PIC X(8)    VALUE  "PRODUCTS".
            05 FILLER                  PIC X(13)   VALUE "SALESPERSONS".
            05 FILLER                  PIC X(2).
            05 FILLER                  PIC X(13)   VALUE "SALESPERSONS".
            05 FILLER                  PIC X(2).
       01   HEADING-LINE3.
            05 FILLER                  PIC X(12).
            05 FILLER                  PIC X(9)    VALUE "TERRITORY".
            05 FILLER                  PIC X(8).
            05 FILLER                  PIC X(6)    VALUE "NUMBER".
            05 FILLER                  PIC X(10).
            05 FILLER                  PIC X(8)   VALUE  "EARNINGS".
            05 FILLER                  PIC X(10).
            05 FILLER                  PIC X(5)    VALUE "BONUS".

       01   DETAIL-LINE.
            05 FILLER                  PIC X(3).
            05 SP-REGION-PRINT         PIC X(2).
            05 FILLER                  PIC X(12).
            05 SP-TERRITORY-PRINT      PIC X(2).
            05 FILLER                  PIC X(15).
            05 SP-NO-PRINT             PIC 9(3).
            05 FILLER                  PIC X(15).
            05 SP-NAME-PRINT           PIC X(25).
            05 FILLER                  PIC X(2).
            05 UNITS-SOLD-PRINT        PIC Z(3).
            05 FILLER                  PIC X(2).
            05 SP-EARNINGS-PRINT       PIC $(5).99.
            05 FILLER                  PIC X(2).
            05 SP-BONUS-PRINT          PIC $(7).99.
            05 FILLER                  PIC X(5).
            05 SP-PAYROLL-PRINT        PIC $(7).99.

       01   ERROR-LINE.
            05 ERROR-MSG               PIC X(60).

       01   TOTAL-LINE.
            05 FILLER                  PIC X(45).
            05 TOT-EARNINGS-PRINT      PIC $(7).99.
            05 FILLER                  PIC X(5).
            05 TOT-BONUS-PRINT         PIC Z(7).99.
            05 FILLER                  PIC X(5).
            05 TOT-PAYROLL-PRINT       PIC $(7).99.

       01   PROGRAM-ACCUMULATORS.
            05 TOT-BONUS               PIC 9(7)V99.
            05 TOT-EARNINGS            PIC 9(7)V99.
            05 TOT-PAYROLL             PIC 9(7)V99.

       01   PROGRAM-WORK-AREAS.
            05 SP-SALES-AMOUNT         PIC 9(5)V99.
            05 SP-BONUS                PIC 9(7)V99.
            05 SP-PAYROLL              PIC 9(7)V99.            

       PROCEDURE DIVISION.
       MAIN-RTN.
            OPEN INPUT  SALES-FILE
                 OUTPUT PRINT-FILE
                        ERROR-REPORT.

            PERFORM READ-REC THRU READ-REC-EXIT UNTIL EOF = "YES".

            PERFORM PRINT-TOTALS.

            CLOSE SALES-FILE
                  PRINT-FILE
                  ERROR-REPORT.
            STOP RUN.

       READ-REC.
            READ SALES-FILE AT END
                 MOVE "YES"    TO EOF
                 GO TO READ-REC-EXIT.

            IF SP-REC-CODE NOT = "25" THEN
               MOVE "ERROR - INVALID RECORD TYPE NOT 25" TO ERROR-MSG
               PERFORM WRITE-ERROR
               GO TO READ-REC-EXIT.

            IF LINE-COUNT = ZEROS OR
               LINE-COUNT > PAGE-SIZE THEN
               PERFORM PRINT-HEADINGS
               MOVE 2         TO LINE-SPACING
            ELSE
               MOVE 1         TO LINE-SPACING.

            PERFORM PRINT-DETAIL-LINE.
       READ-REC-EXIT.
            EXIT.

       PRINT-DETAIL-LINE.
            MOVE SP-REGION    TO SP-REGION-PRINT.
            MOVE SP-TERRITORY TO SP-TERRITORY-PRINT.
            MOVE SP-NO        TO SP-NO-PRINT.
            MOVE SP-NAME      TO SP-NAME-PRINT.
            MOVE UNITS-SOLD   TO UNITS-SOLD-PRINT.           

            COMPUTE SP-SALES-AMOUNT = UNITS-SOLD * UNIT-PRICE.
            ADD SP-SALES-AMOUNT TO TOT-EARNINGS.

            IF TOT-EARNINGS =  100000  AND
               TOT-EARNINGS <  500000  THEN
               COMPUTE SP-BONUS = SP-SALES-AMOUNT *  2.5 /  100.

            IF TOT-EARNINGS > 500000  AND
               TOT-EARNINGS < 1000000 THEN
               COMPUTE SP-BONUS = SP-SALES-AMOUNT *  5 /  100.

            IF TOT-EARNINGS > 1000000 THEN
               COMPUTE SP-BONUS = SP-SALES-AMOUNT * 10 /  100.  

            ADD SP-BONUS          TO TOT-BONUS.
            COMPUTE TOT-PAYROLL = SP-SALES-AMOUNT + SP-BONUS.

            MOVE SP-SALES-AMOUNT TO SP-EARNINGS-PRINT.
            MOVE SP-BONUS        TO SP-BONUS-PRINT.
            MOVE SP-PAYROLL      TO SP-PAYROLL-PRINT.
            WRITE PRINT-REC FROM TOTAL-LINE AFTER ADVANCING LINE-SPACING.
            ADD LINE-SPACING     TO LINE-COUNT.
            MOVE ZEROS           TO PROGRAM-WORK-AREAS.

       PRINT-HEADINGS.
            WRITE PRINT-REC FROM HEADING-LINE1 AFTER ADVANCING PAGE.
            WRITE PRINT-REC FROM HEADING-LINE2 AFTER ADVANCING 1 LINE.
            WRITE PRINT-REC FROM HEADING-LINE2 AFTER ADVANCING 1 LINE.
            WRITE PRINT-REC FROM HEADING-LINE1 AFTER ADVANCING 1 LINE.
            ADD 7 TO LINE-COUNT.
       PRINT-TOTALS.

            MOVE TOT-PAYROLL   TO TOT-PAYROLL-PRINT.
            MOVE TOT-BONUS     TO TOT-BONUS-PRINT.
            MOVE TOT-PAYROLL   TO TOT-PAYROLL-PRINT.
            WRITE PRINT-REC  FROM TOTAL-LINE AFTER ADVANCING 2 LINES.

       WRITE-ERROR.
            WRITE ERROR-REC  FROM ERROR-LINE AFTER ADVANCING 1 LINE.

This is the first program, if this help students am willing to post more sample programs code. All the Division names and Paragraphs start at column 8 of your editor. All the COBOL statements start at column 12 of your editor. The WordPress don’t let me define the proper spacing. Make your own standards of aligning picture clause at column 40, indent the Procedure division statements.

Read SALES-FILE AT END

———->MOVE “YES” TO EOF.

OR

IF RECORD-CODE NOT = “25”

–>MOVE “ERROR” TO ERR-MSG

—>PERFORM ERROR-RTN

END-IF. (End If may not be recognized properly with your PC compilers like Fijistu or Micro Focus COBOL.

Following is the input file SALESEDIT.TXT.

25NW0100014DEVOR, ERNEST N A0075 755 00282840003771220003000000
10NW3800015LYKE, SHARON SHARA A1250 030 00187310000149850000090000
25NW3800016LATING, CAL Q A1125 142 00797950000709290001400000
abNW3800017KATED, ED U A0975 276 01344150001378620001000000
25NE3500005YOUZERS, BILL D A1300 796 05168830003976020001900000
25NE3500006WAGNER, GUY A1250 746 04657840003726270004000000
25SW0400042WILMER, PATRICE A1175 815 04783340004070925003000000
10SW0400043MYLES, IDA WALKER A1350 7!0 05057440003746250004400000
25SW0500044TIME, NICK A A1275 005 00031840000024975000040000
25SW0500045THRUST, PERRY N A1525 523 03983890002612385002200000
25SW0500046HUMPHREY, STAN A0800 249 00995000001243755002400000
15SW0500047LOGAN, KRISTIN I1050 393 02061190001963035001500000
25SW0500048NATION, BERTHA V C0925 028 00129370000139860000100000
25SW0500049SCHULTZ, CHARLES I1150 184 01056940000919080000950000
00SW0500050CARVER, GEORGE A147a 8oo 05894100003996000003996000
25SW0500051CARTOZIAN, ARAM A1325 199 01317060000994005020000000
25SW3300052FORSYTHE, ROBERT ALEXANDERAq805 592 02380420002957040001000000
25NE3500007HAMILTON, BRUCE A0975 263 01280840001313680002600000
25NE3500008FITTS, BEN E C1425 902 06420320004505490003000000
25NE2000001PICATTA, BEVERLY A1025 290 01510360001473520000700000
25NE2000002LEAR, CRYSTAL SHANDA A1550 891 06898340004450540003000000
25NE2000003PAPPAS, JENNIFER A0875 060 00262240000299700000300000
25NE3500009PARSONS, LAURIE A1000 457 02282720002282710002300000
25NE3900010SNOW, MEREDITH A0850 202 00857640001008990000900000
25NE3900011WILLING, ABEL N A0925 826 03816430004125870002100000
25NE3900012DAY, ONA CLAIRE A1105 403 02224350002012980002500000
NE4000013FOREVAR, YUKON C A1300 570 03701300002847150002600000
25NW4800018CALLAHAN, KATHLEEN A1505 240 01804190001198800002200000
25NW4800019ERMINE, URA PLACE A1450 665 04816430003321670001800000
25NW4800020SANDERS, GARY A0800 998 03988010004985010005000000
25NW4800021ENDICOTT, PAMELA C1325 536 03547450002677320002500000
25NW4800022DICKENS, GIVEN D A1075 485 02604270002422570004800000
25MW1500023BENEDICT, HAMMOND X A0900 033 00148350000164830000200000
25MW1500024HASTE, MARY N A1505 550 04134610002747250001300000
25MW1600025YARBOROUGH, JEFFREY C1225 099 00605770000494500000800000
25MW1700026ABINGTON, CARL A0850 330 01401100001648350001000000
25MW1700027STRUCKSON, EVA D I1475 672 04951040003356640003500000
25MW5100028ASHTON, WILLIAM A1105 001 00055190000049950000020000
25MW5100029YOUNG, ROGER A1000 239 01193810001193800002000000
25SE2400030DANCE, JUAN A I1000 239 01193810001193800002000000
25SE1000031WOOD, U BETTY A1050 028 00146850000139860001200000
25SE1000032BUSTER, PHIL A A0875 640 02797200003196800003300000
25SE1000033QUICK, DEVORS M A1100 484 02659340002417580004800000
25SE1100034WORTHIT, HART LEE A1025 235 01203170001173825000500000
25SE1100035STEWART, JANET A1250 680 04245750003396600006600000
25SE1100036NEAR, SUE V A1575 592 04657340002957040002800000
25SE1800037ZEOM, JIM NAY A0900 562 02526470002807190001500000
25SE1800038COLLINS, BRUCE C1350 311 02097150001553445002000000
25SE2800039REILLY, JACK A1425 173 01231390000864135000400000
25SE2800040COOPER, ROXANNE A1005 983 04934640004910085005000000
25SE2800041DOLLAR, SHURR RINCON A0000 000 00000000000399600000000000
25SW3300053HARRIS, SEAN A1050 654 03430070003266730003000000
25SW3350054ALVAREZ, ELENA A1175 140 00821680000699300001380000
25SW4400055KNEAD, PHIL A A0900 755 03394100003771225003200000
25SW4400056XAVIER, FRANCIS A1525 068 00517980000339660000160000
25SW4400057LANDUSKEY, FRED A1350 209 01409340001043955002000000
25SW4400058KLINE, I D A1400 723 05055940003611385001800000
25SW4400059HELM, SIGRID A1205 615 03701670003071925001500000
25SW4400060BYRD, EARL E I0009 626 00028140003126870006000000

Advertisements

10 Responses to “Sample COBOL Progam For Students.”

  1. Anonymous said

    Um, Upon compiling it generated an error:

    Error accessing file: SALESEDIT.TXT
    SALESRPT Segment RT : Error 013 at COBOL PC 0317
    Description : File not found (Error 013)

  2. QB said

    Looked at the program and SALESEDIT.TXT is defined in Fine Section as SALES-FILE.

    What COBOL Compiler you are using?

  3. QB said

    Thee is also two smiley faces in Picture Clause. “Products” PICTURE is X(Eight) and “BONUS” PICTURE is X(FIVE). WordPress text editor translated that into smiley faces.

  4. CT said

    pls help to show sample cobol as/400 program to add record into existing program. tq

  5. QB said

    CT,

    The AS/400 COBOL program will be similar if it is batch processing. AS/400 COBOL program will be little different if adding program interactive. COBOL interactive program will have to read the SDA format to read the input fields and write it to database.

    If you want interactive program than give me the fields which are needed to add record. Will try to give you program this weekend.

  6. ZAWMAUNGMAUNGMYO said

    CT,
    There is show nothing.please,tell me to see the program after running it.
    my compiler is idecobol.

  7. QB said

    Zaw

    Make sure that you have your input data at the right place. Also this is report program, the output will be on printer.

    QB

  8. nitlogic said

    what is the main feature of cobol ?

  9. prashanth said

    pls post a program in cobol which include all the functions in cobol…
    such as with arrays with perform,file handling with dynamic(sequential + dynamic) and other functions also…
    it may be useful for me if use call and sort…
    pls post …
    i need it..
    thanks in advance.

    • QB said

      Prashanth,

      Arrays or Tables are easy to handle. You define Table in your working storage, make it indexed so you can use Search ALL function.

      The indexed file update / add are done with Write / Rewrite statements.

      I hope this will help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: