You are on page 1of 30

SL.NO. PROGRAM NAME PAGE NO.

01. PROGRAM TO CONVERT SECONDS INTO HOURS, 2


MINUTES AND SECONDS.

02. PROGRSM TO REVERSE A GIVEN NUMBER. 3

03. PROGRAM TO ILLUSTRATE THE USE OF ALL THE 4


EDITING CHARACTERS.

04. PROGRAM TO CALCULATE SUM OF THE DIGITS OF N 5


DIGIT NUMBER.

05. PROGRAM TO PRINT THE FIBONACCI SEQUENCE 6-7


BETWEEN M AND N.

06. PROGRAM TO FIND THE LARGEST AND ITS POSITION 8-9


FROM THE GIVEN SET OF NUMBERS.

07. PROGRAM TO GENERATE THE PRIME NUMBERS 10-11


BETWEEN M AND N.

08. PROGRAM TO CALCULATE THE MATRIX ADDITION 12-14


AND SUBTRACTION.

09. PROGRAM TO CALCULATE THE FACTORIAL USING A 15


SUBROUTINE.

10. PROGRAM TO COUNT THE NUMBER OF OCCURRENCES 16


OF SPECIFIED CHARACTERS FROM THE GIVEN STRING,
REPLACE THE SPECIFIED CHARACTER WITH THE
GIVEN CHARACTER.

11. PROGRAM TO CREATE A SEQUENTIAL FILE. 17-18

12. PROGRAM TO CREATE A RELATIVE FILE. 19-20

13. PROGRAM TO SORT A SEQUENCE FILE. 21-23

14. PROGRAM TO MERGE TWO SEQUENTIAL FILES. 24-27

15. PROGRAM FOR INDEXED FILE UPDATION. 28

1
♣♣♣PROGRAM TO CONVERT SECONDS INTO HOURS, MINUTES AND
SECONDS. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. PRO1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 SEC PIC 9(5).
77 MIN PIC 9(2).
77 HR PIC 9(2).
77 SE PIC Z9(2).
PROCEDURE DIVISION.
PARA-1.
DISPLAY(3,1) "ENTER THE TOTAL TIME IN SECOND ".
ACCEPT(3,32) SEC.
DIVIDE 60 INTO SEC GIVING MIN REMAINDER SEC.
DIVIDE 60 INTO MIN GIVING HR REMAINDER MIN.
MOVE SEC TO SE.
DISPLAY " ".
DISPLAY "HOURS = ", HR.
DISPLAY "MINUTES = ", MIN.
DISPLAY "SECONDS = ", SE.

DISPLAY(8,3) "TIME = ", HR, " : ", MIN, " : ", SE.
STOP RUN.

OUTPUT:

ENTER THE TOTAL TIME IN SECOND 6547


HOURS = 01
MINUTES = 31
SECONDS = 18

TIME = 01 : 31 : 18

2
♣♣♣ PROGRSM TO REVERSE A GIVEN NUMBER. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. PRO2.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM PIC 9(5).
77 REV PIC 9(5).
77 REM PIC 99.
PROCEDURE DIVISION.
PARA-1.
DISPLAY(3,5) "ENTER ANY NUMBER ".
ACCEPT(3,22) NUM.
PERFORM PARA-2 UNTIL (NUM<=0)
DISPLAY(5,5) "REVERSE OF NUMBER = ", REV.
STOP RUN.

PARA-2.
DIVIDE 10 INTO NUM GIVING NUM REMAINDER REM.
COMPUTE REV = (REV * 10) + REM.

OUTPUT:
1. ENTER ANY NUMBER 89745

REVERSE OF NUMBER = 54798

2. ENTER ANY NUMBER 76245

REVERSE OF NUMBER = 54267

3
♣♣♣PROGRAM TO ILLUSTRATE THE USE OF ALL THE EDITING
CHARACTERS. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. EDITING.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM PIC S9(6).
77 AST PIC *****9.
77 DOL PIC $99,999.
77 MIN PIC -99999.
77 PLU PIC +99999.
77 CRE PIC 99999CR.
77 DEC PIC ZZZ9.99.
77 BLK PIC 9B9B9B9B9.
77 ZEO PIC 909900.
77 SLS PIC 99/99/99.
77 BLK1 PIC ZZZ9.99 BLANK WHEN ZERO.
77 NUM1 PIC ZZZZZ9.
PROCEDURE DIVISION.
DISPLAY "ENTER THE VALUE: ".
ACCEPT NUM.
MOVE NUM TO NUM1 AST DOL MIN PLU CRE DEC BLK ZEO BLK1 SLS.
DISPLAY "IN ZERO SEPERATION:",NUM1.
DISPLAY "IN ASTRIC SIGN:",AST.
DISPLAY "IN CURRENCY SIGN:",DOL.
DISPLAY "IN MINUS SIGN:",MIN.
DISPLAY "IN PLUS SIGN:",PLU.
DISPLAY "IN CREDIT:",CRE.
DISPLAY "IN DECIMAL:",DEC.
DISPLAY "IN BLANK INSERTION:",BLK.
DISPLAY "IN ZERO INSERTION:",ZEO.
DISPLAY "IN BLANK WHEN ZERO:",BLK1.
DISPLAY "IN SLASH FORM:",SLS.

OUTPUT:

1. ENTER THE VALUE:


+4768
IN ZERO SEPERATION: 4768
IN ASTRIC SIGN:**4768
IN CURRENCY SIGN:$04,768
IN MINUS SIGN: 04768
IN PLUS SIGN:+04768
IN CREDIT:04768
IN DECIMAL:4768.00
IN BLANK INSERTION:0 4 7 6 8
IN ZERO INSERTION:706800
IN BLANK WHEN ZERO:4768.00
IN SLASH FORM:00/47/68

4
♣♣♣ PROGRAM TO CALCULATE SUM OF THE DIGITS OF N DIGIT
NUMBER. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. PRO5.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM PIC 9(7).
77 SUMS PIC 9(5).
77 REM PIC 99.
PROCEDURE DIVISION.
PARA-1.
DISPLAY(3,5) "ENTER ANY NUMBER ".
ACCEPT(3,25) NUM.
PERFORM PARA-2 UNTIL(NUM<=0)
DISPLAY(5,5) "SUM OF DIGITS = ", SUMS.
STOP RUN.
PARA-2.
DIVIDE 10 INTO NUM GIVING NUM REMAINDER REM.
COMPUTE SUMS = SUMS + REM.

OUTPUT:
1. ENTER ANY NUMBER 65874
SUM OF DIGITS = 30

2. ENTER ANY NUMBER 4578


SUM OF DIGITS = 24

5
♣♣♣PROGRAM TO PRINT THE FIBONACCI SEQUENCE BETWEEN
M AND N. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. PRO6.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 M PIC 999.
77 N PIC 999.
77 A PIC S9(3) VALUE IS -1.
77 B PIC 9(3) VALUE IS 1.
77 TOT PIC 9(3).
PROCEDURE DIVISION.
PARA-1.
DISPLAY "ENTER THE STARTING VALUE ".
ACCEPT M.
DISPLAY "ENTER THE FINAL VALUE ".
ACCEPT N.
DISPLAY " ".
IF(M>N)
DISPLAY "FIRST NUMBER IS GREATER PLEASE RE-ENTER THE
VALUE "
DISPLAY " "
GO TO PARA-1.
DISPLAY "FIBONACCI SERIES BETWEEN ", M, " AND ", N, " ARE :-"
PERFORM PARA-2 UNTIL(TOT>N)
STOP RUN.
PARA-2.
COMPUTE TOT = A + B.
MOVE B TO A.
MOVE TOT TO B.
IF(TOT>=M AND TOT<=N)
DISPLAY TOT.

OUTPUT:

1. ENTER THE STARTING VALUE


78
ENTER THE FINAL VALUE
36

FIRST NUMBER IS GREATER PLEASE RE-ENTER THE VALUE

6
ENTER THE STARTING VALUE
5
ENTER THE FINAL VALUE
100
FIBONACCI SERIES BETWEEN 5 AND 100 ARE :-
005
008
013
021
034
055
089

7
♣♣♣PROGRAM TO FIND THE LARGEST AND ITS POSITION FROM
THE GIVEN SET OF NUMBERS. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. PRO7.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ARR-REC.
02 ARR PIC 9(3) OCCURS 10 TIMES.
77 POS PIC 99.
77 LARG PIC 9(3).
77 N PIC 99.
77 CNT PIC 99 VALUE IS 1.
77 CHS PIC X VALUE IS "Y".
PROCEDURE DIVISION.
PARA-1.
PERFORM PARA-2 UNTIL(CHS="N")
MOVE ARR(1) TO LARG.
MOVE 1 TO POS.
MOVE CNT TO N.
MOVE 1 TO CNT.
PERFORM PARA-3 UNTIL(CNT>=N)
DISPLAY "THE LARGEST NUMBER IS = ", LARG.
DISPLAY "POSITION OF LARGEST NUMBER = ", POS.
STOP RUN.
PARA-2.
DISPLAY "ENTER ANY NUMBER ".
ACCEPT ARR(CNT).
ADD 1 TO CNT.
DISPLAY "DO YOU WANT TO COUNTINUE (Y/N)".
ACCEPT CHS.
PARA-3.
IF(ARR(CNT)>LARG)
MOVE ARR(CNT) TO LARG
MOVE CNT TO POS.
COMPUTE CNT = CNT + 1.

OUTPUT:
ENTER ANY NUMBER
45
DO YOU WANT TO COUNTINUE (Y/N)
Y
ENTER ANY NUMBER
22
DO YOU WANT TO COUNTINUE (Y/N)
Y
ENTER ANY NUMBER
63

8
DO YOU WANT TO COUNTINUE (Y/N)
Y
ENTER ANY NUMBER
49
DO YOU WANT TO COUNTINUE (Y/N)
N

THE LARGEST NUMBER IS = 063


POSITION OF LARGEST NUMBER = 03

9
♣♣♣PROGRAM TO GENERATE THE PRIME NUMBERS BETWEEN
M AND N. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. PRO8.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 M PIC 9(3).
77 N PIC 9(3).
77 REM PIC 99.
77 CNT PIC 9(3).
PROCEDURE DIVISION.
PARA-1.
DISPLAY "ENTER STARTING VALUE ".
ACCEPT M.
DISPLAY "ENTER FINAL VALUE".
ACCEPT N.
IF(M>N)
DISPLAY "WRONG ENTRY PLEASE RE-ENTER THE VALUES"
GO TO PARA-1.
DISPLAY "PRIME NUMBER BETWEEN ", M, " AND ", N, " ARE :-".
PERFORM PARA-2 UNTIL(M>N)
STOP RUN.
PARA-2.
MOVE 2 TO CNT.
PERFORM PARA-3 UNTIL(CNT>=M)
IF(M>1 AND M<=N)
DISPLAY M.
COMPUTE M = M + 1.
PARA-3.
DIVIDE CNT INTO M GIVING REM REMAINDER REM.
IF(REM=0 AND M>2)
GO TO PARA-4
ELSE
COMPUTE CNT = CNT + 1.
PARA-4.
COMPUTE M = M + 1.
GO TO PARA-2.

OUTPUT:

ENTER STARTING VALUE


13
ENTER FINAL VALUE
6
WRONG ENTRY PLEASE RE-ENTER THE VALUES

10
ENTER STARTING VALUE
3
ENTER FINAL VALUE
50

PRIME NUMBER BETWEEN 3 AND 50 ARE :-


003
005
007
011
013
017
019
023
029
031
037
041
043
047

11
♣♣♣PROGRAM TO CALCULATE THE MATRIX ADDITION AND
SUBTRACTION. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TAB1.
02 A1 OCCURS 5 TIMES.
03 A PIC S99 OCCURS 5 TIMES.
01 TAB2.
02 B1 OCCURS 5 TIMES.
03 B PIC S99 OCCURS 5 TIMES.
01 TAB3.
02 SUM-T OCCURS 5 TIMES.
03 SUM-TAB PIC S99 OCCURS 5 TIMES.
01 TAB4.
02 SUB-T OCCURS 5 TIMES.
03 SUB-TAB PIC S99 OCCURS 5 TIMES.

77 R PIC 9.
77 C PIC 9.
77 I PIC 99.
77 J PIC 99.
77 ROWS PIC 99.
77 COLS PIC 99.
77 SOUT PIC -Z99.
PROCEDURE DIVISION.
PARA1.
DISPLAY(3, 5) "ENTER HOW MANY ROWS : ".
ACCEPT(3, 28) R.
DISPLAY(4, 5) "ENTER HOW MANY COLS : ".
ACCEPT(4, 28) C.
DISPLAY(6, 5) "ENTER THE ELEMENTS OF THE TWO MATRICES .....".
DISPLAY(7,5) "MATRIX A".
DISPLAY(8,5) "--------".
DISPLAY(7,25) "MATRIX B".
DISPLAY(8,25) "--------".
MOVE 9 TO ROWS.
MOVE 5 TO COLS.
PERFORM PARA2 VARYING I FROM 1 BY 1 UNTIL(I>R) AFTER J FROM
1 BY 1 UNTIL(J>C)
MOVE 9 TO ROWS.
MOVE 25 TO COLS.
PERFORM PARA3 VARYING I FROM 1 BY 1 UNTIL(I>R) AFTER J FROM
1 BY 1 UNTIL(J>C)
DISPLAY(15,5) "(A+B)MATRIX".
DISPLAY(16,5) "-----------".
DISPLAY(15,25) "(A-B)MATRIX".

12
DISPLAY(16,25) "-----------".
MOVE 17 TO ROWS.
MOVE 5 TO COLS.
PERFORM PARA4 VARYING I FROM 1 BY 1 UNTIL(I>R) AFTER J FROM
1 BY 1 UNTIL(J>C)
MOVE 17 TO ROWS.
MOVE 25 TO COLS.
PERFORM PARA5 VARYING I FROM 1 BY 1 UNTIL(I>R) AFTER J FROM
1 BY 1 UNTIL(J>C)
STOP RUN.
PARA2.

ACCEPT(ROWS,COLS) A(I,J).
MOVE A(I,J) TO SOUT.
DISPLAY(ROWS,COLS) SOUT.

ADD 4 TO COLS.
IF J=C
MOVE 5 TO COLS
ADD 1 TO ROWS.
PARA3.
ACCEPT(ROWS,COLS) B(I,J).
MOVE B(I,J) TO SOUT.
DISPLAY(ROWS,COLS) SOUT.
ADD 4 TO COLS.
IF J=C
MOVE 25 TO COLS
ADD 1 TO ROWS.

PARA4.
COMPUTE SUM-TAB(I,J) = A(I,J) + B(I,J).
MOVE SUM-TAB(I,J) TO SOUT.
DISPLAY(ROWS,COLS) SOUT.

ADD 4 TO COLS.
IF J=C
MOVE 5 TO COLS
ADD 1 TO ROWS.

PARA5.
COMPUTE SUB-TAB(I,J) = A(I,J) - B(I,J).
MOVE SUB-TAB(I,J) TO SOUT.
DISPLAY(ROWS,COLS) SOUT.
ADD 4 TO COLS.
IF J=C
MOVE 25 TO COLS
ADD 1 TO ROWS.

13
OUTPUT:

ENTER HOW MANY ROWS : 2


ENTER HOW MANY COLS : 3

ENTER THE ELEMENTS OF THE TWO MATRICES....:-

MATRIX A MATRIX B
---------------- ---------------
02 05 06 -07 08 09
-04 06 08 01 05 03

(A+B) MATRIX (A-B) MATRIX


--------------------- ---------------------
-05 13 15 09 -03 -03
-03 11 11 -05 01 05

14
♣♣♣PROGRAM TO CALCULATE THE FACTORIAL USING A
SUBROUTINE. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. FAC.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM PIC 9(2).
PROCEDURE DIVISION.
PARA-1.
DISPLAY(3,5) "ENTER ANY TWO DIGIT NUMBER ".
ACCEPT(3,33) NUM.
CALL "SUBFAC" USING NUM.
STOP RUN.

IDENTIFICATION DIVISION.
PROGRAM-ID. SUBFAC.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 RES PIC 9(10) VALUE IS 1.
77 ANS PIC ZZZZZ999.
LINKAGE SECTION.
77 A PIC 9(2).
PROCEDURE DIVISION USING A.
PARA-1.
PERFORM PARA-2 UNTIL (A<1)
MOVE RES TO ANS.
DISPLAY(4,5) "FACTORIAL OF GIVEN NUMBER = ",

ANS.
PARA-2.
COMPUTE RES = RES * A.
COMPUTE A = A - 1.

OUTPUT:
ENTER ANY TWO DIGIT NUMBER 6
FACTORIAL OF GIVEN NUMBER = 720

ENTER ANY TWO DIGIT NUMBER 5


FACTORIAL OF GIVEN NUMBER = 120

15
♣♣♣PROGRAM TO COUNT THE NUMBER OF OCCURRENCES OF
SPECIFIED CHARACTERS FROM THE GIVEN STRING, REPLACE THE
SPECIFIED CHARACTER WITH THE GIVEN CHARACTER. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID STR.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 FIND1 PIC A.
77 REPLACE1 PIC A.
77 COUNT1 PIC 99 VALUE IS 0.
77 I PIC 99 VALUE IS 1.
01 SEN.
05 STR PIC X OCCURS 50 TIMES.
PROCEDURE DIVISION.
DISPLAY(3,5) "ENTER THE STRING :-".
ACCEPT SEN.
DISPLAY(4,5) "ENTER CHARACTER TO BE SEARCHED :".
ACCEPT(4,36) FIND1.
DISPLAY(5,5) "ENTER THE CHARACTER TO BE INSERTED ".
ACCEPT(5,40) REPLACE1.
PERFORM PARA-1 VARYING I FROM 1 BY 1 UNTIL(I>50)
DISPLAY(7,2) "AFTER REPLACING THE CHARACTER THE STRING IS ".
DISPLAY " ".
DISPLAY SEN.
DISPLAY "TOTAL OCCURENCE IS = ", COUNT1.
STOP RUN.
PARA-1.
IF STR(I)=FIND1
COMPUTE COUNT1 = COUNT1 + 1
MOVE REPLACE1 TO STR(I).

OUTPUT:

1. ENTER THE STRING :- WELCOME USER


ENTER CHARACTER TO BE SEARCHED : Z
ENTER THE CHARACTER TO BE INSERTED : A
AFTER REPLACING THE CHARACTER THE STRING IS
WELCOME USER
TOTAL OCCURENCE IS = 00

2. ENTER THE STRING :- COBOL IS A LANGUAGE.


ENTER CHARACTER TO BE SEARCHED : G
ENTER THE CHARACTER TO BE INSERTED : Z
AFTER REPLACING THE CHARACTER THE STRING IS
COBOL IS A LANZUAZ.
TOTAL OCCURENCE IS = 02

16
♣♣♣PROGRAM TO CREATE A SEQUENTIAL FILE. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. SEQFILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DANFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD DANFILE
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "DANTEST.TXT"
DATA RECORD IS DANREC.
01 DANREC.
02 NAME PIC A(10).
02 ADDR PIC X(20).
02 SAL PIC 9(8).
WORKING-STORAGE SECTION.
77 CHS PIC X VALUE IS "Y".
PROCEDURE DIVISION.
PARA-1.
OPEN OUTPUT DANFILE.
PERFORM PARA-2 UNTIL(CHS="N")
DISPLAY "A SEQUENTIAL FILE 'DANTEST.TXT' CREATED......".
CLOSE DANFILE.
STOP RUN.
PARA-2.
DISPLAY "ENTER THE NAME ".
ACCEPT NAME.
IF NAME IS NOT ALPHABETIC
DISPLAY "NAME IS INVALID."
GO TO PARA-2.
DISPLAY "ENTER THE ADDRESS".
ACCEPT ADDR.
DISPLAY "ENTER THE SALARY".
ACCEPT SAL.
WRITE DANREC.
DISPLAY "DO YOU WANT TO CONTINUE (Y/N) ".
ACCEPT CHS.

17
OUTPUT:

ENTER THE NAME


DANISH123
NAME IS INVALID.
ENTER THE NAME
DANISH
ENTER THE ADDRESS
BANGLORE
ENTER THE SALARY
34525
DO YOU WANT TO CONTINUE (Y/N)
Y

ENTER THE NAME


RAHUL
ENTER THE ADDRESS
DELHI
ENTER THE SALARY
32125
DO YOU WANT TO CONTINUE (Y/N)
N

A SEQUENTIAL FILE 'DANTEST.TXT' CREATED......

IN FILE “DANTEST.TXT” THERE IS WRITTEN AS

DANISH BANGLORE 00034525


RAHUL DELHI 00032125

18
♣♣♣PROGRAM TO CREATE A RELATIVE FILE. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. RELFILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DANRELFILE ASSIGN TO DISK
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD DANRELFILE
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "RELFILE.TXT"
DATA RECORD IS DANREC.
01 DANREC.
02 NAME PIC A(10).
02 ADDR PIC X(20).
02 SAL PIC 9(8).
WORKING-STORAGE SECTION.
77 CHS PIC X VALUE IS "Y".
PROCEDURE DIVISION.
PARA-1.
OPEN OUTPUT DANRELFILE.
PERFORM PARA-2 UNTIL(CHS="N")
DISPLAY "A RELATIVE FILE 'RELFILE.TXT' CREATED......".
CLOSE DANRELFILE.
STOP RUN.
PARA-2.
DISPLAY "ENTER THE NAME ".
ACCEPT NAME.
IF NAME IS NOT ALPHABETIC
DISPLAY "NAME IS INVALID."
GO TO PARA-2.
DISPLAY "ENTER THE ADDRESS".
ACCEPT ADDR.
DISPLAY "ENTER THE SALARY".
ACCEPT SAL.
WRITE DANREC INVALID KEY DISPLAY "...INVALID RECORD...".
DISPLAY "DO YOU WANT TO CONTINUE (Y/N) ".
ACCEPT CHS.

19
OUTPUT:

ENTER THE NAME


SAKET12

NAME IS INVALID.
ENTER THE NAME
SAKET
ENTER THE ADDRESS
PATNA
ENTER THE SALARY
54321
DO YOU WANT TO CONTINUE (Y/N)
Y

ENTER THE NAME


ABHIJIT
ENTER THE ADDRESS
KOLKATA
ENTER THE SALARY
23141
DO YOU WANT TO CONTINUE (Y/N)
N

A RELATIVE FILE 'RELFILE.TXT' CREATED......

IN FILE “RELFILE.TXT” THERE IS WRITTEN AS

SAKET PATNA 00054321


ABHIJIT KOLKATA 00023141

20
♣♣♣ PROGRAM TO SORT A SEQUENCE FILE. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MYFILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
SELECT SORT-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
SELECT W-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD MYFILE
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "MY.TXT"
DATA RECORD IS MY-REC.
01 MY-REC.
02 ENAME PIC A(20).
02 EADDR PIC X(20).
02 ESAL PIC 9(6).
FD SORT-FILE
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "MY-SORT.TXT"
DATA RECORD IS S-REC.
01 S-REC.
02 SNAME PIC A(20).
02 SADDR PIC X(20).
02 SSAL PIC 9(6).
SD W-FILE.
01 W-REC.
02 WNAME PIC A(20).
02 WADDR PIC X(20).
02 WSAL PIC 9(6).
WORKING-STORAGE SECTION.
01 D-REC.
02 NAME PIC A(20).
02 ADDR PIC X(20).
02 SAL PIC 9(6).
77 CHS PIC X VALUE IS "Y".

PROCEDURE DIVISION.
PARA-1.
OPEN OUTPUT MYFILE.
PERFORM PARA-2 UNTIL(CHS="N")
CLOSE MYFILE.
MOVE "Y" TO CHS.

21
DISPLAY "SORTED CONTENTS ARE GIVEN BELOW".

SORT W-FILE ON ASCENDING KEY WNAME USING MYFILE GIVING


SORT-FILE.

OPEN INPUT SORT-FILE.

PERFORM UNTIL(CHS="N") READ SORT-FILE AT END MOVE "N" TO


CHS NOT AT END MOVE S-REC TO D-REC DISPLAY
D-REC.

CLOSE SORT-FILE.
STOP RUN.
PARA-2.
DISPLAY "ENTER THE NAME ".
ACCEPT ENAME.
IF ENAME IS NOT ALPHABETIC
DISPLAY "INVALID NAME"
GO TO PARA-2.
DISPLAY "ENTER THE ADDRESS ".
ACCEPT EADDR.
DISPLAY "ENTER THE SALARY ".
ACCEPT ESAL.
WRITE MY-REC.
DISPLAY "DO YOU WANT TO ADD MORE RECORD ? (Y/N) ".
ACCEPT CHS.

OUTPUT:

ENTER THE NAME


2RAHUL

INVALID NAME.
ENTER THE NAME
RAHUL
ENTER THE ADDRESS
BANGLORE
ENTER THE SALARY
23451
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
Y

ENTER THE NAME


DANISH
ENTER THE ADDRESS
PATNA
ENTER THE SALARY
45231
DO YOU WANT TO ADD MORE RECORD ? (Y/N)

22
Y

ENTER THE NAME


AMIT
ENTER THE ADDRESS
DELHI
ENTER THE SALARY
23145
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
N

SORTED CONTENTS ARE GIVEN BELOW


AMIT DELHI 023145
DANISH PATNA 045231
RAHUL BANGLORE 023451

IN FILE “MY-SORT.TXT” THERE IS WRITTEN AS


AMIT DELHI 023145
DANISH PATNA 045231
RAHUL BANGLORE 023451

23
♣♣♣PROGRAM TO MERGE TWO SEQUENTIAL FILES. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. MERG-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MYFILE1 ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
SELECT MYFILE2 ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
SELECT M-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL
SELECT W-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD MYFILE1
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "MY1.TXT"
DATA RECORD IS M-REC1.
01 M-REC1.
02 MNAME1 PIC A(15).
02 MADDR1 PIC X(20).
02 MSAL1 PIC 9(6).
FD MYFILE2
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "MY2.TXT"
DATA RECORD IS M-REC2.
01 M-REC2.
02 MNAME2 PIC A(15).
02 MADDR2 PIC X(20).
02 MSAL2 PIC 9(6).

FD M-FILE
LABEL RECORDS ARE STANDERED
VALUE OF FILE-ID IS "MERG-FILE.TXT"
DATA RECORD IS MERG-REC.
01 MERG-REC.
02 MNAME PIC A(15).
02 MADDR PIC X(20).
02 MSAL PIC 9(6).

SD W-FILE.
01 W-REC.
02 WNAME PIC A(15).
02 WADDR PIC X(20).
02 WSAL PIC 9(6).

24
WORKING-STORAGE SECTION.

01 D-REC.
02 DNAME PIC A(15).
02 DADDR PIC X(20).
02 DSAL PIC 9(6).
77 CHS PIC X VALUE IS "Y".
PROCEDURE DIVISION.
PARA-1.
DISPLAY "ENTER THE CONTENT OF FIRST FILE".
OPEN OUTPUT MYFILE1.
PERFORM PARA-2 UNTIL(CHS="N")
CLOSE MYFILE1.
DISPLAY " ".
DISPLAY "FIRST FILE IS CREATED SUCCESSFULLY........".
MOVE "Y" TO CHS.
DISPLAY " ".
DISPLAY "ENTER THE CONTENT OF SECOND FILE".
OPEN OUTPUT MYFILE2.
PERFORM PARA-3 UNTIL(CHS="N")
CLOSE MYFILE2.
DISPLAY " ".
DISPLAY "SECOND FILE IS CREATED SUCCESSFULLY........".
DISPLAY " ".
DISPLAY "MERGED RECORDS ARE GIVEN BELOW".
MOVE "Y" TO CHS.

MERGE W-FILE ON ASCENDING KEY WNAME USING MYFILE1


MYFILE2 GIVING M-FILE.

OPEN INPUT M-FILE.

PERFORM UNTIL(CHS="N") READ M-FILE AT END MOVE "N" TO CHS NOT


AT END MOVE MERG-REC TO D-REC DISPLAY D-REC.

CLOSE M-FILE.
STOP RUN.
PARA-2.
DISPLAY "ENTER THE NAME".
ACCEPT MNAME1.
IF MNAME1 IS NOT ALPHABETIC
DISPLAY "NAME IS INVALID"
GO TO PARA-2.
DISPLAY "ENTER THE ADDRESS".
ACCEPT MADDR1.
DISPLAY "ENTER THE SALARY".
ACCEPT MSAL1.
WRITE M-REC1.
DISPLAY "DO YOU WANT TO ADD MORE RECORD ? (Y/N)".
ACCEPT CHS.

25
PARA-3.
DISPLAY "ENTER THE NAME".
ACCEPT MNAME2.
IF MNAME2 IS NOT ALPHABETIC
DISPLAY "NAME IS INVALID"
GO TO PARA-3.
DISPLAY "ENTER THE ADDRESS".
ACCEPT MADDR2.
DISPLAY "ENTER THE SALARY".
ACCEPT MSAL2.
WRITE M-REC2.
DISPLAY "DO YOU WANT TO ADD MORE RECORD ? (Y/N)".
ACCEPT CHS.

OUTPUT:

ENTER THE CONTENT OF FIRST FILE


ENTER THE NAME
DANISH
ENTER THE ADDRESS
PATNA
ENTER THE SALARY
43521
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
Y

ENTER THE NAME


DANISH
ENTER THE ADDRESS
PATNA
ENTER THE SALARY
43521
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
N
FIRST FILE IS CREATED SUCCESSFULLY……….

ENTER THE CONTENT OF FIRST FILE


ENTER THE NAME
RAHUL
ENTER THE ADDRESS
KOLKATA
ENTER THE SALARY
32412

26
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
Y

ENTER THE NAME


ABHIJIT
ENTER THE ADDRESS
BANGLORE
ENTER THE SALARY
21432
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
N
SECOND FILE IS CREATED SUCCESSFULLY……….

MERGED RECORDS ARE GIVEN BELOW


DANISH PATNA 043521
AMIT DELHI 023143
RAHUL KOLKATA 032412
ABHIJIT BANGLORE 021432

IN FILE “MY1.TXT” THERE IS WRITTEN AS

DANISH PATNA 043521


AMIT DELHI 023143

IN FILE “MY2.TXT” THERE IS WRITTEN AS

RAHUL KOLKATA 032412


ABHIJIT BANGLORE 021432

IN FILE “MERG-FILE.TXT” THERE IS WRITTEN AS

DANISH PATNA 043521


AMIT DELHI 023143
RAHUL KOLKATA 032412
ABHIJIT BANGLORE 021432

27
♣♣♣ PROGRAM FOR INDEXED FILE UPDATION. ♣♣♣

IDENTIFICATION DIVISION.
PROGRAM-ID. UPDATE-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MAIN-FILE ASSIGN TO DISK
ORGANIZATION IS INDEXED
RECORD KEY IS ITEM-NO
ACCESS MODE IS RANDOM.
DATA DIVISION.
FILE SECTION.
FD MAIN-FILE
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "MYUPDT.TXT"
DATA RECORD IS MAIN-REC.
01 MAIN-REC.
02 ITEM-NO PIC 9(5).
02 ITEM-NAME PIC X(10).
02 ITEM-PRICE PIC 9(6).
02 STOCK PIC 9(5).
WORKING-STORAGE SECTION.
77 FIELD PIC X(12).
77 ANS PIC X(1) VALUE "Y".
PROCEDURE DIVISION.
PARA-1.
OPEN OUTPUT MAIN-FILE.
PERFORM PARA-2 UNTIL(ANS="N")
CLOSE MAIN-FILE.
MOVE "Y" TO ANS.
OPEN I-O MAIN-FILE.
PERFORM PARA-3 UNTIL(ANS="N")
CLOSE MAIN-FILE.
STOP RUN.
PARA-2.
DISPLAY "ENTER ITEM-NO".
ACCEPT ITEM-NO.
DISPLAY "ENTER ITEM-NAME".
ACCEPT ITEM-NAME.
DISPLAY "ENTER ITEM-PRICE".
ACCEPT ITEM-PRICE.
DISPLAY "ENTER THE STOCK".
ACCEPT STOCK.
WRITE MAIN-REC.
DISPLAY "DO YOU WANT TO ADD MORE RECORD ? (Y/N)".
ACCEPT ANS.

28
PARA-3.
DISPLAY "ENTER THE RECORD-NO TO BE MODIFIED".
ACCEPT ITEM-NO.
READ MAIN-FILE INVALID KEY DISPLAY "RECORD NOT
FOUND" GO TO PARA-3.

DISPLAY "GIVE THE FIELD TO BE MODIFIED".


ACCEPT FIELD.
IF(FIELD="ITEM-NAME")
DISPLAY "GIVE ITEM-NAME"
ACCEPT ITEM-NAME
ELSE IF(FIELD="ITEM-NO")
DISPLAY "GIVE ITEM-NO"
ACCEPT ITEM-NO
ELSE IF(FIELD="ITEM-PRICE")
DISPLAY "GIVE ITEM-PRICE"
ACCEPT ITEM-PRICE
ELSE IF(FIELD="STOCK")
DISPLAY "GIVE THE STOCK VALUE"
ACCEPT STOCK
ELSE
DISPLAY "WRONG FIELD NAME ".

REWRITE MAIN-REC INVALID KEY DISPLAY "KEY NOT FOUND".


DISPLAY "DO YOU WANT TO UPDATE MORE RECORDS (Y/N): ".
ACCEPT ANS.
IF ANS="N"
DISPLAY "RECORDS MODIFIED....".

OUTPUT:

ENTER ITEM-NO
21
ENTER ITEM-NAME
LUX
ENTER ITEM-PRICE
19
ENTER THE STOCK
545
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
Y

ENTER ITEM-NO
12
ENTER ITEM-NAME
SURF EXCEL
ENTER ITEM-PRICE
54
ENTER THE STOCK
612
29
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
Y

ENTER ITEM-NO
14
ENTER ITEM-NAME
BREAD
ENTER ITEM-PRICE
11
ENTER THE STOCK
45
DO YOU WANT TO ADD MORE RECORD ? (Y/N)
N

ENTER THE RECORD TO BE MODIFIED


14
GIVE THE FIELD TO BE MODIFIED
ITEM-NAME
GIVE ITEM-NAME
AMUL BREAD
DO YOU WANT TO UPDATE MORE RECORDS (Y/N):
Y
ENTER THE RECORD TO BE MODIFIED
14
GIVE THE FIELD TO BE MODIFIED
STOCK
GIVE THE STOCK VALUE
145
DO YOU WANT TO UPDATE MORE RECORDS (Y/N):
N

RECORDS MODIFIED…….

BEFORE UPDATE, IN FILE “MYUPDT.TXT” THERE IS WRITTEN AS

00021 LUX 000019 00545

00012 SURF EXCEL 000054 00612

00014 BREAD 000011 00045

AFTER UPDATE, IN FILE “MYUPDT.TXT” THERE IS WRITTEN AS

00021 LUX 000019 00545

00012 SURF EXCEL 000054 00612

00014 AMUL BREAD 000011 00145

30