Thursday, December 29, 2016

File Handling questions with its solutions

1.   Write a program in QBASIC to open a sequential data file “EMP.DAT”, which contains employees records: Name, address and phone number and display all the records as well as total number of records stored in the file.    
                                                                      
OPEN “EMP.DAT” FOR INPUT AS #1
CLS
PRINT “NAME”, “ADDRESS”, “PHONE NUMBER”
C=0
WHILE NOT EOF(1)
INPUT #1, N$, A$, P#
PRINT N$, A$, P#
C=C+1
WEND
PRINT “TOTAL NUMBER OF RECORDS”; C
CLOSE #1
END

2.   Write a program to store records regarding the information of Book number, Book’s name and Writer’s name in a sequential data file called “Library.dat”.

OPEN “Library.dat” FOR OUTPUT AS #1
 DO
CLS
INPUT “ENTER BOOK’S NUMBER”; BN
INPUT “ENTER BOOK’S NAME”; B$
INPUT “ENTER WRITER’S NAME”; N$
WRITE #1, BN, B$, N$
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END

3.   Write a program to create a sequential data file “Employee.Dat” to store employees’ name, address, age, gender and salary.

OPEN “Employee.dat” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER EMPLOYEE’S NAME”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER AGE”; A
INPUT “ENTER GENDER”; G$
INPUT”ENTER SALARY”; S
WRITE #1, N$, A$, A, G$, S
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END

4.   A sequential data file “EMP.DAT” contains name, post and salary fields of information about employees. Write a program to display all the information of employees along with tax amount (also tax is 15% of salary).

OPEN “EMP.DAT” FOR INPUT AS #1
CLS
PRINT “NAME”, “POST”, “SALARY”, “TAX”
WHILE NOT EOF(1)
INPUT #1, N$, P$, S
T = 15 / 100 * S
PRINT N$, P$, S, T
WEND
CLOSE #1
END

5.    A sequential data file called “student.dat” contains some records under the field’s name, English, Nepali and Computer. Write a program to add some more records in the same sequential data file.
     
OPEN “student.dat” FOR APPEND AS #1
DO
CLS
INPUT “ENTER NAME”; N$
INPUT “ENTER MARKS IN ENGLISH”; E
INPUT “ENTER MARKS IN NEPALI”; N
INPUT “ENTER MARKS IN COMPUTER”; C
WRITE #1, N$, E, N, C
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END

6.    Write a program to create a data file ‘teldir.dat’ to store Name, Address and Telephone number of employees according to the need of the user.

OPEN “teldir.dat” FOR OUTPUT AS #1
DO      
CLS
INPUT “ENTER NAME”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER TELEPHONE NUMBER”; T#
WRITE #1, N$, A$, T#
INPUT “DO YOU WANT TO CONTINUE(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) = ”Y”
CLOSE #1
END

7.   Write a program to create a data file ‘teldir.dat’ to store Name, Address and Telephone number of employees according to the need of the user.

OPEN “teldir.dat” FOR OUTPUT AS #1
DO      
CLS
INPUT “ENTER NAME”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER TELEPHONE NUMBER”; T#
WRITE #1, N$, A$, T#
INPUT “DO YOU WANT TO CONTINUE(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) = ”Y”
CLOSE #1
END

8.   A sequential data file called “Marks.dat” contains Name, English, Nepali, Maths and Science Fields. Write a program to display all the contents of that data file.      

OPEN “Marks.dat” FOR INPUT AS #1
CLS
PRINT “NAME”, “ENGLISH”, “NEPALI”, “MATHS”, “SCIENCE”
WHILE NOT EOF(1)
INPUT #1, N$, E, N, M, S
PRINT N$, E, N, M, S
WEND
CLOSE #1
END

9.   A sequential data file named “Nabil.txt” contains record of clients of a bank including depositor’s name, deposited amount, time and rate of interest. WAP to display detail of all depositors including simple interest.                                                                             

OPEN "Nabil.txt" FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, D$, A, T, R
S=(A*T*R)/100
PRINT D$, A, T, R, S
WEND
CLOSE #1
END

10.           A sequential data file named “Nabil.txt” contains record of clients of a bank including depositor’s name, deposited amount, time and rate of interest. WAP to display detail of all depositors including simple interest.

OPEN "Nabil.txt" FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, D$, A, T, R
S=(A*T*R)/100
PRINT D$, A, T, R, S
WEND
CLOSE #1
END

11.           Write a program to create a sequential data file “RESULT.DAT” to store name, address and marks obtained in 3 different subjects of students.

OPEN “LIB.TXT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter name”; N$
INPUT “Enter address”; A$
INPUT “Enter marks in three subjects”; M1, M2, M3
WRITE #1, N$, A$, M1, M2, M3
INPUT “Do you want to continue?”; CH$
LOOP WHILE UCASE$(CH$) = “Y “
CLOSE #1
END

12.           Write a program to search and display only those records whose percentage is more than 60% from the data file “RESULT.DAT” which contains Student’s Symbol number, Date of Birth, Total Marks, Percentage and Division.                                               

 OPEN  “RESULT.DAT” FOR INPUT AS  #1
CLS
WHILE NOT EOF(1)
INPUT #1 , SN , D$ , M  , P , DV$
IF P> 60 THEN PRINT SN , D$ , M , P ,DV$
WEND
CLOSE #1
END

13.           A data file named “EMP.DAT” contains some records with fields Code, Name, Post and Salary. Write a program to print odd position records of the data file.                     

OPEN “EMP.DAT” FOR INPUT AS #1
CLS
Print “Name”, “Code”,”Post”,”Salary”
C=0
WHILE NOT EOF(1)
INPUT #1, CO, N$, P$, S
C=C+1
IF C MOD 2 = 1 THEN PRINT CO, N$, P$, S
End if
WEND
CLOSE #1
END

14.           A data file named “Staff.dat” contains staff name, Department, Post and Salary of some staffs. Write a program to count and display total number of records   in a file.   

OPEN “STAFF.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$,D$,P$,S
C=C+1
WEND
PRINT “Total Number of Records=”;C
CLOSE #1
END
15.           A sequential data file “Staff.dat” contains the name, address, post and salary of the employees. Write a program to read and display all the records stored in the above data file.

 OPEN “STAFF.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A$, P$, S
PRINT N$, A$, P$, S
WEND
CLOSE #1
END

16.           Write a program to view those records from “Employee.dat” sequential data file having employee’s name, department, appointment data and salary whose salary is more than Rs.5000.

OPEN “EMPLOYEE.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, D$, AD$, S
IF S > 5000 THEN PRINT N$, D$, AD$, S
WEND
CLOSE #1
END

17.           A sequential data file called ‘MARKS.DAT’ contains ROLL NO, NAME, English, Nepali and Maths fields. Write a program to display all the contents of the data file.

OPEN “MARKS.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, RN, N$, E, N, M
PRINT RN, N$, E, N, M
WEND
CLOSE #1
END

18.           A data file “LIB.TXT” consists of book’s name, author’s name and price of books. Write a program to count and display the total number of records present in the file.

OPEN “LIB.TXT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A$, P
C=C+1
WEND
PRINT “The total number of records=”; C
CLOSE #1
END

19.           A sequential data file called “Marks.dat” contains roll number, name, English, Nepali, and Maths field. Write a program to display all the content of the data file.

OPEN “MARKS.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, RN, N$, E, N, M
PRINT RN, N$, E, N, M
WEND
CLOSE #1
END

20.             A sequential data file named”rec.dat”contains name,post and salary.  Write a program to display all the records for the employees whose Salary is more than 8000.

OPEN “REC.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, P$, S
IF S>8000 THEN PRINT N$, P$, S
WEND
CLOSE #1
END
-----All the Best-----


No comments:

Post a Comment