Вы находитесь на странице: 1из 17

EXPERIMENT NO.

1 AIM: INTRODUCTION TO COBOL COBOL CODING SHEET:;


COBOL is a high level language and therefore a COBOL source program must be written in a format acceptable to the COBOL compiler. Precisely for this reason, COBOL programs are written on COBOL coding sheets, which use a standard format. There are 80 characters positions on each line of the coding sheet and these positions are grouped into the following five fields Positions 1.6 7 8.11 12-72 72.80 fields sequence indicator area A/margin A area B/margin B Identification

Position 8-72 actually contains the COBOL code. This area is divided into two fields/area A and area B (also known as margin A and margin B). COBOL requires that some of the entries must in area A and others must be confined to area B only. The sequence field may be used to assign sequence numbers to the coding lines. This field is optional and, if desired, may be left unused. When used, the sequence number must be in the ascending order but need not be consecutive. Usually, page number is written in the first three positions (1 to 3) and line numbers are written in the remaining three positions (4 to 6). The COBOL compiler, in addition to its normal work, can optionally produce a printed copy source program being compiled. This copy is known as source listing. Anything written in the identification field, will be ignored by he compiler but appear in the source listing. The indicator field may contain an asterisk (*), a slash (/) or a hyphen (-). An asterisk or a slash denotes that the line under consideration is a comment line. The compiler ignores the comment line, but it appears in the source listing. The performer is free to write anything in a comment line, but generally notes indicating the intentions of the programmer are included in the comment lines. A comment line can appear anywhere after the first line of the COBOL program. The difference between the asterisk (*) and slash (/) is that in the case of the latter, the comment line will be printed after the page injection (i.e., after skipping to the top of the next page). No such page ejection will take place in the case of an asterisk (*).

DIVISIONS AND SECTIONS OF COBOL PROGRAM:


COBOL program is categorized into four main Divisions. Each Division contains further

one or two sections. The declaration of various divisions and sections are as follows:

1. IDENTIFICATION DIVISION
The IDENTIFICATION DIVISION is the first division of every COBOL source program. There may be several paragraphs in this division of which the paragraph PROGRAM-ID is essential in most of the machines..The other paragraphs are optional and may be used mainly for documentation purposes. The following shows the structure of this division:

IDENTIFICATION DIVISION. PROGRAM-ID. entry. [AUTHOR. entry.] [INSTALLATION. entry.] [DATE-WRITTEN. entry.] [DATE-COMPILED. entry.]
The division heading and paragraph names should be coded as area A entries. Each of the paragraph names must end with a period followed by at least one blank space. The entries following the paragraph headings must be terminated by a period. The entry in the PROGRAM-ID contains the program name to be used to identify the object program. The program name can consist of 1 to 30 characters and must contain at least one letter located in any position within the name. However, these rules can vary depending on the compiler to be used.

2. ENVIRONMENT DIVISION
The ENVIRONMENT DIVISION is the division that must follow the IDENTIFICTION DIVISION in a COBOL source program. Among all the four divisions this one is the most machine-dependent division. The computer and all peripheral devices required by the program are described in this division. This division contains two sections:- CONFIGURATION SECTION and INPUTOUTPUT SECTION. Of these, the CONFIGURATION SECTION appears first. The outline of the sections and paragraphs of this division is shown below:

ENVIRONMENT DIVISION.

CONFIGURATION SECTION. SOURCE-COMPUTER. source-computer-entry. OBJECT-COMPUTER. object-computer-entry. [SPECIAL NAMES. special-names-entry] [INPUT-OUTPUT SECTION. FILE-CONTROL. {file-control-entry} . [IO-CONTROL. input-output-control-entry].]
For most compilers the COBOL, source program must at least include the two section headings and three paragraphs-SOURCE-COMPUTER, OBJECT-COMPUTER and FILECONTROL. The division headings, section headings and the paragraph headings should be coded as area A entries. The paragraph headings should be coded as area entries. The paragraph headings must be followed by a period and then a space. The entries in the paragraphs are area B entries and can start in the same line with the paragraph heading. 1.1 CONFIGURATION SECTION This section contains an overall specifications of the computer used for the purpose of compilation and execution of the program. There are in all three paragraphs in this section: SOURCE-COMPUTER This paragraph specifies the name of the computer used to compile the COBOL program. The following is the form of this paragraph: SOURCE-COMPUTER. computer-name. For example, If ICL 1901 is to be used for compiling the COBOL source program, this paragraph should be as follows: SOURCE-COMPUTER.ICL-1901. OBJECT-COMPUTER The OBJECT-COMPUTER paragraph describes the computer on which the program is to be executed. The following shows the syntax of this paragraph: OBJECT-COMPUTER. computer -name [, MEMORY SIZE integer-1 {CHARACTERS} ] { WORDS} [, PROGRAM COLLATING SEQUENCE IS alphabetname] 3

[, SEGMENT-LIMIT IS integer-2]. The COMPUTER NAME specifies a particular computer on which the object program is to be executed. MEMORY SIZE It is used to indicate the amount of storage for the object program. PROGRAM COLLATING SEQUENCE CLAUSE It specifies the collating sequence that is used to compare non-numeric data items. SEGMENT-LIMIT This clause is used in most of the compilers to indicate that thesections having segment number less than the number specified in integer-2 should beheld in permanent area of storage and should not be transferred to and from thevirtual memory. For example: OBJECT-COMPUTER.ICL-1900. MEMORY SIZE 8000 WORDS. 1.2 INPUT-OUTPUT SECTION This section contains information regarding files to be used in the program. There are two paragraphs in this section:- FILE-CONTROL and I-O-CONTROL. FILE-CONTROL The FILE-CONTROL paragraph names each file and identifies the file medium through file control entries. The simplified format of a file control entry is given below: SELECT [OPTIONAL] file-name ASSIGN TO hardware-name. For example: FILE-CONTROL. SELECT CARD-DESIGN ASSIGN TO READER. SELECT PRINTER-FILE ASSIGN TO PRINTER. This paragraph indicates that there are two files- CARD-DESIGN and PRINTER-FILE. The file named CARD-DESIGN is a card file while the other is report file to be printed on a line printer.

3. DATA DIVISION
The DATA DIVISION is that part of a COBOL program where every data item processed by the program is described. It is important to note that unless a data-item is described in the data division, it cannot be used in the procedure division. The data 4

division is divided into a number of sections and depending on the use of data item, it should be defined in the appropriate section. For the time being, only two of the sections of the DATA DIVISION will be considered. The format of DATA DIVISION is as follows:

DATA DIVISION. [FILE SECTION. File section entries. ] [WORKING -STORAGE SECTION. Working-storage entries. ] FILE SECTION
This FILE SECTION includes the descriptions of all data items that should be read from or written onto some external file. The FILE SECTION must contain file description entry followed by one or record description entries for each of the files used in the program. FD file-name LABEL {RECORDS ARE} {STANDARD} {RECORD IS} {OMITTED} WORKING STORAGE SECTION The data items which are developed internally as intermediate results as well as constants, are described in this section of the DATA DIVISION.

4. PROCEDURE DIVISION
The PROCEDURE DIVISION contains statements, which specify the operations to be performed by the computer. Each of these statements is formed with COBOL words and literals. A statement always starts with the COBOL verb. The following format shows the simplified structure of the PROCEDURE DIVISION when it contains sections:

PROCEDURE DIVISION. {section-name SECTION. {paragraph-name.[sentence].]}.


The following format shows the simplified structure of the PROCEDURE-

PROCEDURE DIVISION. [paragraph-name. [sentence].] PARA-ONE.

EXPERIMENT:2 AIM:WRITE A PROGRAM TO PERFORM ADDITION, SUBTRACTION, MULTIPLICATION AND DIVISION. ADDITION
IDENTIFICATION DIVISION. PROGRAM-ID. ENVIRONMENT DIVISION. DATA DIVISIION. WORKING-STORAGE SECTION. 01 A PIC 99 VALUE 40. 01 B PIC 99 VALUE 30. 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY"1ST NO". DISPLAY"2ND NO". ADD A B GIVING C. DISPLAY"RESULT IS"C. STOP RUN.

OUTPUT:

SUBTRACTION
IDENTIFICATION DIVISION. PROGRAM-ID. ENVIRONMENT DIVISION. DATA DIVISIION. WORKING-STORAGE SECTION. 01 A PIC 99 . 01 B PIC 99 . 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "1ST NO". DISPLAY "2ND NO" . ADD A FROM B GIVING C. DISPLAY "RESULT OF SUBTRACTION IS" C. STOP RUN.

OUTPUT:

MULTIPLICATION
IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 B PIC 99. 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "1ST NO" . ACCEPT A. DISPLAY "2ND NO" . ACCEPT B. MULTIPLY A BY B GIVING C. DISPLAY "RESULT BY MULTIPLYING A AND B IS" " " C. STOP RUN.

OUTPUT:

DIVISION
IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 B PIC 99. 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "1ST NO" . ACCEPT A. DISPLAY "2ND NO" . ACCEPT B. DIVIDE A BY B GIVING C. DISPLAY "DIVIDING A BY B IS" " " C. STOP RUN.

OUTPUT:

EXPERIMENT :3 AIM:WRITE A PROGRAM TO PERFORM ARITHMETIC OPERATIONS USING IF-ELSE AND COMPUTE
IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 B PIC 99. 01 C PIC 99. 01 CH PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "1ST NO" . ACCEPT A. DISPLAY "2ND NO" . ACCEPT B. DISPLAY "CHOICE" . ACCEPT CH. IF CH IS EQUAL TO 1 COMPUTE C = A + B DISPLAY "ADDING A AND B ANS IS" " " C ELSE IF CH IS EQUAL TO 2 COMPUTE C = A - B DISPLAY "SUBTRACTING A AND B ANS IS" " " C ELSE IF CH IS EQUAL TO 3 COMPUTE C = A * B DISPLAY "MULTIPLYING A AND B ANS IS" " " C ELSE COMPUTE C = A / B DISPLAY "DIVIDING A BY B ANS IS" " " C. STOP RUN.

OUTPUT:

10

EXPERIMENT:4 AIM :WRITE A PROGRAM TO CHECK NO IS EVEN OR ODD


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 B PIC 99. 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY " ENTER NO" . ACCEPT A. DIVIDE A BY 2 GIVING B REMAINDER C . IF C IS EQUAL TO 1 DISPLAY " NUMBER IS ODD " ELSE DISPLAY " NO IS EVEN ". STOP RUN.

OUTPUT:

EXPERIMENT:5
11

AIM:WRITE A PROGRAM TO FIND NO IS PRIME OR NOT


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 B PIC 99. 01 C PIC 99. 01 D PIC 99. 01 R PIC 99. 01 CNT PIC 99 VALUE IS 0. PROCEDURE DIVISION. PARA-1. DISPLAY "ENTER THE NUMBER: ". ACCEPT A. COMPUTE C = A / 2. PERFORM PARA-2 VARYING B FROM 2 BY 1 UNTIL B > C. IF CNT = 0 DISPLAY "NUMBER IS PRIME." ELSE DISPLAY "NUMBER IS NOT PRIME.". STOP RUN. PARA-2. DIVIDE A BY B GIVING D REMAINDER R. IF R = 0 COMPUTE C = C + 1.

OUTPUT:

EXPERIMENT:6

12

AIM:WRITE A PROGRAM TO FIND FACTORIAL OF A NO:


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 99. 01 I PIC 99 VALUE IS 01. 01 FAC PIC 99 VALUE IS 0001. PROCEDURE DIVISION. PARA-1. DISPLAY "1ST NO" . ACCEPT N. PERFORM PARA-2 VARYING I FROM N BY -1 UNTIL I = 1. DISPLAY "FACTORIAL OF A NO IS"" " FAC. STOP RUN. PARA-2. COMPUTE FAC = FAC * I . OUTPUT:

EXPERIMENT:7
13

AIM:WRITE A PROGRAM TO DISPLAY FIBONACCI SERIES


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99 VALUE IS 00. 01 B PIC 99 VALUE IS 01. 01 C PIC 99 VALUE IS 1. 01 N PIC 99. 01 I PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "1ST NO" . ACCEPT N. DISPLAY "FIBONACCI SERIES IS ". DISPLAY "0" . DISPLAY "1". PERFORM PARA-2 VARYING I FROM 3 BY 1 UNTIL I > N. STOP RUN. PARA-2. DISPLAY C. COMPUTE A = B . COMPUTE B = C . COMPUTE C = A + B .

OUTPUT:

EXPERIMENT:8
14

AIM:WRITE A PROGRAM TO DISPLAY TEMP IN FARENHIET AND CELCIUS


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 TF PIC 99 VALUE 0. 01 TC PIC 99 VALUE 0. 01 T PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "ENTER TEMP IN CELCIUS" . ACCEPT T. COMPUTE TF = ( 1.8 * T ) + 32 . DISPLAY "TEMP IN FAHRENHEIT =" TF. DISPLAY "ENTER TEMP IN FAHRENHIET" . ACCEPT T. COMPUTE TC = 0.56 * ( T - 32 ). DISPLAY "TEMP IN CELSIUS =" TC. STOP RUN.

OUTPUT:

EXPERIMENT:9

15

AIM:WRITE A PROGRAM TO FIND GREATEST OF 3 NOS


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 B PIC 99. 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "ENTER 1st NO" . ACCEPT A. DISPLAY "ENTER 2nd NO" . ACCEPT B. DISPLAY "ENTER 3rd NO" . ACCEPT C. IF A IS GREATER THAN B IF A IS GREATER THAN C DISPLAY " GREATEST NO IS " A ELSE DISPLAY "GREATEST NO IS " C ELSE IF B IS GREATER THAN C DISPLAY " GREATEST NO IS " B ELSE DISPLAY "GREATEST NO IS " C . STOP RUN.

OUTPUT:

EXPERIMENT:10

16

AIM:WRITE A PROGRAM TO FIND AVERAGE MARKS


IDENTIFICATION DIVISION. PROGRAM-ID. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 99. 01 S PIC 99 VALUE IS 0. 01 N PIC 99. 01 AVG PIC 99. 01 C PIC 99. PROCEDURE DIVISION. PARA-1. DISPLAY "ENTER NO OF SUBJECTS" . ACCEPT N. PERFORM PARA-2 VARYING C FROM 1 BY 1 UNTIL C>N. COMPUTE AVG = S / N . DISPLAY "AVG IS " AVG. STOP RUN. PARA-2. DISPLAY "MARKS OF SUBJECTS". ACCEPT A. COMPUTE S = S + A.

OUTPUT:

17

Вам также может понравиться