The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Allstar Technology, 2020-06-04 03:08:52

New gateway computer 10press-2077 final

New gateway computer 10press-2077 final

g. DECLARE SUB TEST (W$)

CLS

INPUT “ENTER A WORD”;A$

CALL TEST (A$)

END

SUB TEST (W$)

FOR K = 1 TO LEN (W$)

U$ = MID$ (W$, K, 1) +U$

NEXT K

PRINT U$

END SUB

i. List out the arguments and parameters used in the above program.

ii. List the library functions used in the above program.
h. DECLARE SUB SUM(N)
INPUT “Enter any number”; N
CALL SUM(N)
END
SUB SUM(N)
S=0
WHILE N<>0
R=N MOD 10
S=S+R
N=N\10
WEND
PRINT “SUM”;S
END SUB
i. In which condition, the statements within the WHILE….WEND looping statement
will not be executed?
ii. Will the output be the same if we place “/” instead of”\” in the above program?

New Gateway to Computer Science Book -10 201

6. Re-write the given program after correcting the bugs.
a. REM to display a word by extracting characters of even position.
CLS
DECLARE SUB POS$(X$, Y$)
INPUT “ENTER A WORD:”; X$
CALL SUB POS$ (X$, Y$)
END
SUB WORD$(X$, Y$)
FOR P = 1 TO LEN (X$) STEP 2
E$ = LEFT$ (X$, 1, P)
WORD$ = E$ + WORD$
NEXT P
Y$ = WORD$
END SUB

b. REM to display a word by extracting characters of even position
CLS
DECLARE SUB NAME$(X$,Y$)
INPUT”ENTER A WORD”;X$
CALL SUB NAME$(X$,Y$)
PRINT Y$
END
SUB WORD$(X$,Y$)
FOR P=1 TO LEN (X$) STEP 2
E$=LEFT$(X$,1,P)
WORD$=E$+WORD$
NEXT P
Y$=WORD$
END SUB

202 New Gateway to Computer Science Book -10

c. DECLARE SUB FIBO ( )
CLS
EXECUTE FIBO
END
SUB FIBO
REM Program to generate 2, 2, 4, 6, 10…upto 10th terms.
A=2
B=2
FOR Ctr = 5 TO 1
DISPLAY A; B;
A=A+B
B=A+B
NEXT Ctr
END FUNCTION

d. DECLARE SUB CUBE(N) e. Series()
CLS CLS
FOR I = 1 TO 5 EXECUTE Series
READ END
CALL CUBE(No) SUB Series()
NEXT X REM program to generate 1 1 2 3 5….
DATA 3, 5, 2, 6, 4 Up to 20th terms
END A=1
SUB CUBE( ) B=1
DISPLAY N^3 FOR ctr= 10 TO 1
END SUB DISPLAY A;B;
A=A+B
B=A+B
EXT ctr
END Series()

New Gateway to Computer Science Book -10 203

Practical Session on FUNCTION Procedure

Program 1
Write a program to calculate the average of three numbers.
DECLARE FUNCTION AVGE (A, B, C)
CLS
INPUT “ENTER FIRST NUMBER”; A
INPUT “ENTER SECOND NUMBER”; B
INPUT “ENTER THIRD NUMBER”; C
AV = AVGE(A, B, C)
PRINT “AVERAGE OF THREE NUMBERS”; AV
END
FUNCTION AVERAGE (A, B, C)
AVGR = (A + B + C) / 3
AVERAGE = AVGR
END FUNCTION
Program 2
Write a program to find the area of four walls.
DECLARE FUNCTION AREA(L,B,H)
CLS
INPUT “ENTER LENGTH” ; L
INPUT “ENTER BREADTH” ; B
INPUT “ENTER HEIGHT” ; H
PRINT “THE AREA OF FOUR WALLS= ” ; AREA(L,B,H)
END
FUNCTION AREA(L,B,H)
AREA=2*H*(L+B)
END FUNCTION
Program 3
Write a program to calculate the circumference of a circle.
DECLARE FUNCTION CIR(A)
CLS

204 New Gateway to Computer Science Book -10

INPUT “enter radius of a circle”; R 205
PRINT “circumference is” ; CIR(R)
END
FUNCTION CIR(A)
P=3.14
CIR=2*P*A
END FUNCTION

Program 4
Write a program to calculate the simple interest.
DECLARE FUNCTION SI(P,T,R)
CLS
INPUT “ENTER PRINCIPLE” ; P
INPUT “ENTER TIME” ; T
INPUT “ENTER RATE” ; R
PRINT “SIMPLE INTEREST IS = ” ;SI(P,T,R)
END
FUNCTION SI(P,T,R)
SI=(P*T*R)/100
END FUNCTION

Program 5
Write a program to convert the Nepalese currency into Indian currency.
DECLARE FUNCTION Convert (N)
CLS
INPUT “Enter NC”; p
PRINT “IC”; Convert(p)
END
FUNCTION Convert (p)
conv = p / 1.6
Convert = conv
END FUNCTION

New Gateway to Computer Science Book -10

Program 6
Write a program to define a function procedure to display the area of sphere [Hint: Area of
sphere: 4*PI*R^2]
DECLARE FUNTION AREA(R)
CLS
CONST PI= 3.14
INPUT “Enter radius”; R
PRINT “The area of sphere=”; AREA(R)
END
FUNCTION AREA(R)
A=4*PI*R^2
AREA=A
END FUNCTION
Program 7
Write a program to enter any 5 numbers and display their product.

DECLARE FUNCTION PRO (N( ))
CLS
FOR I = 1 TO 5
INPUT “ENTER THE NUMBERS”; N(I)
NEXT I
PRINT “PRODUCT OF 5 NUMBERS”; PRODUCT (N( ))
END
FUNCTION PRO (N())
FOR I = 1 TO 5
P = P * N(I)
NEXT I
PRO = P
END FUNCTION

Program 8
Write a program to calculate the volume of a cylinder using [Hint: PI*R^2*H]
DECLARE FUNCTION VOL(R,H)
CLS
CONST PI=3.14

206 New Gateway to Computer Science Book -10

INPUT “Enter radius and height”; R, H 207
PRINT “The volume=”; VOL(R,H)
END
FUNCTION VOL(R,H)
V=PI*R^2*H
VOL=V
END FUNCTION

Program 9
Write a program to display the greater number among any two numbers.
DECLARE FUNCTION GREAT(A,B)
CLS
INPUT “Enter any two number”; A,B
PRINT “The greater number is”; GREAT(A,B)
END
FUNCTION GREAT(A,B)
IF A>B THEN
GREAT= A
ELSE
GREAT=B
END IF
END FUNCTION

Program 10
Write a program to check whether an input number is even or odd.
DECLARE FUNCTION CONV$(N)
CLS
INPUT “ENTER A NUMBER ” ; P
PRINT CONV$(P)
END
FUNCTION CONV$(P)
IF P MOD 2 = 0 THEN
CONV$= “EVEN NUMBER”

New Gateway to Computer Science Book -10

ELSE
CONV$=“ODD NUMBER”
ENDIF
END FUNCTION

Program 11
Write a program that asks any three numbers and displays the difference between the
greatest and the smallest among the three numbers.
DECLARE FUNCTION DIF(A,B,C)
CLS
INPUT “Enter three numbers”; A, B, C
PRINT “THE DIFFERENCE=”; DIF(A, B, C)
END
FUNCTION DIF (A, B, C)
G=0
S=0
IF A > B AND A > B THEN
G=A
ELSEIF B > A AND B > C THEN
G=B
ELSE
G=C
END IF
IF A < B AND A < C THEN
S=A
ELSEIF B < A AND B < C THEN
S=B
ELSE
S=C
END IF
DIF = G – S
END FUNCTION

208 New Gateway to Computer Science Book -10

Program 12
Write a program to display vowels and consonants of the entered string.
DECLARE FUNCTION VOW(S$)
DECLARE FUNCTION CON(S$)
CLS
INPUT “Enter a word”; S$
VO= VOW(S$)
CO= CON(S$)
END
FUNCTION VOW(S$)
PRINT “The vowels are”
FOR I = 1 TO LEN(S$)
B$=MID$(S$,I,1)
C$=UCASE$(B$)
IF C$= “A” OR C$= “E” OR C$= “I” OR C$= “O” OR C$=”U” THEN PRINT B$
NEXT I
END FUNCTION
FUNCTION CON(S$)
PRINT “The consonant are”
FOR I = 1 TO LEN(S$)
B$=MID$(S$,I,1)
C$= UCASE$(B$)
IF C$<> “A” AND C$<> “E” AND C$<> “I” AND C$<> “O” AND C$<> “U” THEN
PRINT B$

NEXT I

END FUNCTION
Program 13
Write a program to check whether the input number is divisible by 3 and 5 or not.
DECLARE FUNCTION CH$(N)
CLS
INPUT “ENTER A NUMBER” ; N
PRINT CH$(N)

New Gateway to Computer Science Book -10 209

END
FUNCTION CH$(N)
IF N MOD 3 =0 AND N MOD 5=0 THEN
CH$= “It is divisible by both numbers”
ELSE
CH$=“It is not divisible by both numbers”
ENDIF
END FUNCTION
Program 14
Write a program to declare a user defined function to return to a given number in reverse
order.
DECLARE FUNCTION REV(N)
CLS
INPUT “Enter a number”; N
PRINT “The reverse form is”; REV(N)
END
FUNCTION REV(N)
S=0
WHILE N<>0
R=N MOD 10
S=S*10+R
N=N\10
WEND
REV=S
END FUNCTION
Program 15
Write a program to print the following series: 1,4,7,……. up to 10th term.
DECLARE FUNCTION SERIES
CLS
D= SERIES
END
FUNCTION SERIES
FOR I = 1 TO 10

210 New Gateway to Computer Science Book -10

PRINT A; 211
A=A+3
NEXT I
END FUNCTION
Program 16
Write a program to input number and find the sum of individual digits.
DECLARE FUNCTION SUM (N)
CLS
INPUT “ENTER ANY NUMBER”; N
SU = SUM (N)
PRINT “SUM OF DIGITS”; SU
END
FUNCTION SUM (N)
S=0
WHILE N < > 0
R = N MOD 10
S=S+R
N = N \ 10
WEND
SUM = S
END FUNCTION
Program 17
Write a program to input number and find the sum of odd digits.
DECLARE FUNCTION SUMODD (N)
CLS
INPUT “ENTER ANY NUMBER”; N
SU = SUMODD (N)
PRINT “SUM OF ODD DIGITS”; SU
END
FUNCTION SUMODD (N)
S=0
WHILE N < > 0
R = N MOD 10

New Gateway to Computer Science Book -10

IF R MOD 2 = 1 THEN S = S + R
N = N \ 10
WEND
SUMODD = S
END FUNCTION
Program 18
Write a program to input a number and count the total number of digits.
DECLARE FUNCTION COUNT (N)
CLS
INPUT “ENTER ANY NUMBER”; N
PRINT “TOTAL NUMBER OF DIGITS”; COUNT (N)
END
FUNCTION SUM (N)
C=0
WHILE N < > 0
C=C+1
N = N \ 10
WEND
COUNT = C
END FUNCTION
Program 19
Write a program to check whether the input number is perfect square or not.
DECLARE FUNCTION square (S)
CLS
INPUT “ENTER ANY NUMBER”; N
S = SQR(N)
PR = square (S)
IF PR = S THEN
PRINT “It is perfect square”
ELSE
PRINT “ It is not perfect square “
END IF
END

212 New Gateway to Computer Science Book -10

FUNCTION square (S)

PERFECT = INT (S)

END FUNCTION

Program 20

Write a program to input a string and count the total number of consonants.
DECLARE FUNCTION COUNT(W$)
CLS
INPUT “Enter a Word”; W$
PRINT “The total number of consonant is”; COUNT(W$)
END
FUNCTION COUNT (W$)
C=0
FOR I = 1 TO LEN(W$)
B$ = MID$(W$, I, 1)
C$ = UCASE$(B$)
IF C$ <> “A” AND C$ <> “E” AND C$ <> “I” AND C$ <> “O” AND C$ <> “U”
THEN C = C + 1
NEXT I
COUNT = C

END FUNCTION

Program 21

Write a program to display H.C.F of any two numbers.

DECLARE FUNCTION HCF(A, B)
CLS
INPUT “ENTER ANY TWO NUMBERS”; A, B
PRINT “H.C.F=”; HCF (A, B)
END
FUNCTION HCF (A, B)
WHILE A MOD B < > 0
T = A MOD B
A=B
B=T
WEND
HCF = B

END FUNCTION

New Gateway to Computer Science Book -10 213

Program 22
Write a program to display the reverse of an input string .
DECLARE FUNCTION REV$(N$)
CLS
INPUT “ENTER A STRING” ; W$
PRINT “REVERSED STRING::”;REV$(W$)
END
FUNCTION REV$(N$)
FOR I = LEN(N$) TO 1 STEP -1
D$=D$+MID$(N$,I,1)
NEXT I
REV$=D$
END FUNCTION
Program 23
WAP to check whether the first character of the input string is a number, an uppercase
or lowercase character or none of them.
DECLARE FUNCTION CHTR$(A$)
CLS
INPUT “ENTER A STRING” ; C$
PRINT CHTR$(C$)
END
FUNCTION CHTR$(A$)
D$=LEFT$(A$)
A=ASC(D$)
IF A>=65 AND A<=90 THEN
CHTR$= “FIRST LETTERIS UPPERCASE LETTER”
ELSEIF A>=97 AND A<=122 THEN
CHTR$=“FIRST LETTER IS LOWERCASE”
ELSEIF A>=48 AND A<=57 THEN
CHTR$=“FIRST LETTER IS NUMBER”
ELSE
CHTR$=”NONE OF THEM”
END FUNCTION

214 New Gateway to Computer Science Book -10

Program 24 215
Write a program to check whether the given number is Armstrong or not.
DECLARE FUNCTION CHECK(N)
CLS
INPUT “Enter any number”; N
A=N
ARM=CHECK (N)
IF A=ARM THEN
PRINT “Armstrong number”
ELSE
PRINT “Not Armstrong number”
END IF
END
FUNCTION CHECK (N)
S=0
WHILE N<>0
R=N MOD 10
S=S+R^3
N=N\10
WEND
CHECK=S
END FUNCTION
Program 25
Write a program to display the factorial of a given number.
DECLARE FUNCTION FACT(N)
CLS
INPUT “Enter any number”; N
PRINT “The factorial is”; FACT (N)
END
FUNCTION FACT (N)
F=1
FOR I=1 TO N

New Gateway to Computer Science Book -10

F=F*I
NEXT I
FACT=F
END FUNCTION
Program 26
Write a program to check whether the given number is prime or composite.
DECLARE FUNCTION PC (N)
CLS
INPUT “Enter any number”; N
P=PC (N)
IF P=2 THEN
PRINT “Prime number”
ELSE
PRINT “Composite number”
END IF
END
FUNCTION PC (N)
C=0
FORI=1 TO N
IF N MOD I=0 THEN C=C+1
NEXT I
PC=C
END FUNCTION
Program 27
Write a program to check whether the entered letter is uppercase or lowercase.
DECLARE FUNCTION UC$ (A$)
CLS
INPUT “Enter a letter”; A$
PRINT UC$(A$)
END
FUNCTION UC$ (A$)
CH$ = UCASE$(A$)
IF A$ = CH$ THEN

216 New Gateway to Computer Science Book -10

UC$ = “It is capital letter” 217
ELSE
UC$ = “It is small letter”
END IF
END FUNCTION
Program 28
Write a program to convert Binary into Decimal.
DECLARE FUNCTION Z (B$)
CLS
INPUT “ENTER A BINARY NUMBER”; B$
PRINT “DECIMAL VALUE IS “; Z(B$)
END
FUNCTION Z (B$)
FOR I = LEN(B$) TO 1 STEP -1
A$ = MID$(B$, I, 1)
C = VAL(A$)
M=M+C*2^P
P=P+1
NEXT I
Z=M
END FUNCTION
Program 29
Write a program to display the following series:
2,1,3,4,7,11,18....up to the 10th term.
DECLARE FUNCTION ser ()
CLS
v = ser
END
FUNCTION ser
a=2
b=1
FOR i = 1 TO 10
PRINT a;

New Gateway to Computer Science Book -10

c=a+b
a=b
b=c
NEXT i
END FUNCTION
Program 30
Write a program to print the following output.
N
NE
NEP
NEPA
NEPAL
DECLARE FUNCTION out ()
CLS
v = out
END
FUNCTION out
S$ = “NEPAL”
FOR i = 1 TO LEN(S$)
PRINT LEFT$(S$, i)
NEXT i
END FUNCTION
Program 31
Write a program to print the series:1,4,7,……. up to 10th term.
DECLARE FUNCTION SERIES
CLS
D= SERIES
END
FUNCTION SERIES
FOR I = 1 TO 10
PRINT A;
A=A+3
NEXT I
END FUNCTION

218 New Gateway to Computer Science Book -10

Program 32
Write a program to check whether the given word is palindrome or not.
DECLARE FUNCTION palindrome$ (w$)
CLS
INPUT “Enter any word”; w$
a$ = w$
IF a$ = palindrome$(w$) THEN
PRINT w$; “ is palindrome word”;
ELSE
PRINT w$; “ is not palindrome word”;
END IF
FUNCTION palindrome$ (w$)
FOR i = LEN(w$) TO 1 STEP -1
rev$ = rev$ + MID$(w$, i, 1)
NEXT i
palindrome$ = rev$
END FUNCTION
Program 33
Write a program to display the given series:
5 10 15 20 25 30 35 40 45 50 …….100
DECLARE FUNCTION series (i)
CLS
FOR i = 5 TO 100 STEP 5
PRINT series(i);
NEXT
END
FUNCTION series (i)
s=s+i
series = s
END FUNCTION

New Gateway to Computer Science Book -10 219

Program 34
Write a program to display the following series:
1 4 7 10 13 16 19
DECLARE FUNCTION series (i)
CLS
FOR i = 1 TO 20 STEP 3
PRINT series(i);
NEXT
FUNCTION series (i)
S=S+i
series = S
END FUNCTION
Program 35
Write a program to display the following pattern:
1 8 27 64 125
DECLARE FUNCTION series (i)
CLS
FOR i = 1 TO 5
PRINT series(i);
NEXT
END
FUNCTION series (i)
s=i^3
series = s
END FUNCTION
Program 36
Write a program that asks series of string value and displays the initial letter from such
word using a function procedure.
For e.g.
Input: Central Processing Unit
Output: CPU


220 New Gateway to Computer Science Book -10

DECLARE FUNCTION initial$ (s$)
CLS
INPUT “Type words “; w$
PRINT initial$(w$)
END
FUNCTION initial$ (s$)
x$ = MID$(s$, 1, 1)
FOR i = 1 TO LEN(s$)

b$ = MID$(s$, i, 1)
IF ASC(b$) = 32 THEN

x$ = x$ + MID$(s$, i + 1, 1)
END IF
NEXT i
initial$ = x$
END FUNCTION

Innovative Task

 Write any ten sub-procedures and ten function-procedures programming in A4
size paper and submit to your teacher as your project work.

Assignment

1. Write a FUNCTION program for the following questions.
a. Write a program to display the product of any three numbers.
b. Write a program to read the length, breadth and height of a box. Calculate its volume
and surface area.
c. Write a program to calculate the average of three numbers.
d. Write a program to get temperature in Celsius from the user and then print the
temperature in Fahrenheit. (Hint: F=9C/5+32).
e. Write a program to get a word from the user and then print it in reverse order.
f. Write a program to input a string and count the total number of consonants.
g. Write a program to get radius of the circle and display the area.
h. Write a program to display the factorial of a given number.
i. Write a program to display the greater number among any two numbers.

New Gateway to Computer Science Book -10 221

j. Write a program to check whether an input number is even or odd.
k. Write a program to check whether the input number is divisible by 5 and 7 or not.
l. Write a program to input number and find the sum of individual digits.
m. Write a program to check whether the number given by the user is positive,
negative or zero.
n. Write a program to display the longest string among three input strings.
o. Write a program to display the sum of numbers from 50 to 100.
p. Write a program to display the first 20 prime numbers.
q. Write a program to count the number of digit in a number entered by the user.
r. Write a program to display the HSTON number as 7 , 22, 11, 34, 17, 52, 26, 13, 40,
20.
s. Write a program to display the given series.
1. 4 , 40, 400, 4000, 40000, 400000
2. 10, 20, 30 , 40 , 50 ………10th terms
3. 5, 10, 15, 20 ,25, 30.
4. 100, 200, 300, 400…… upto 20th terms.
5. 1000, 900, 800, 700…..100.
6. 4 , 8, 12, 16, 20 upto 10th terms.
7. 1,2,4,8,16,32………….10th terms.

2. Write a program to display the given pattern.

a. 1 b. 5 5 5 5 5 5

1 2 5 5 5 5 5

1 2 3 5 5 5 5

1 2 3 4 5 5 5

1 2 3 4 5 5 5

1 2 3 4 5 6 5



c. 1 2 3 4 5 d. L

1 2 3 4 AL

1 2 3 PAL

1 2 E PAL

1 N E PAL

222 New Gateway to Computer Science Book -10

e. C O M P U T E R f. D H A D I N G
C O M P U T E HADING
C O M P U T ADING
C O M P U DING
C O M P ING
C O M NG
C O G
C
g. P R O G R A M M I N G

R O G R A M M I N
OGRAMMI

GRAMM
RAM
A

3. Write the output of the given program.
a. DECLARE FUNCTION TEST(N)
CLS
FOR J=3 TO 9 STEP 2
X= TEST(J)
PRINT X
NEXT J
END
FUNCTION TEST(N)
TEST= (N^2)+2
END FUNCTION

New Gateway to Computer Science Book -10 223

c. DECLARE FUNCTION Interest(p,t,r) d. DECLARE FUNCTION AREA(L,B)

CLS CLS

LET p=30 LET L =100

LET t=40 LET B = 20

LET r=6 LET ans = AREA(L,B)

LET d=Interest(p,t,r) PRINT “The area=”,ans

PRINT “The simple interest will be”; d END

END FUNCTION AREA(L,B)

FUNCTION Interest(p,t,r) ar=L*B

answer = (p*t*r)/100 AREA=ar

Interest=answer END FUNCTION

END FUNCTION

e. DECLARE FUNCTION xyz (n)
CLS
FOR i = 1 TO 5
READ n
z = xyz(n)
s = s + z
NEXT
PRINT s
DATA 10 , 13 , 15 , 4 , 6
END
FUNCTION xyz (n)
IF n MOD 2 = 0 THEN
xyz = n
END IF
END FUNCTION

224 New Gateway to Computer Science Book -10

4. Re-write the given programs after correcting the bugs.

a. DECLARE FUNCTION xyz$ (N$) b. DECLE FUNCTION CIR(A)

CLS CLS

INPUT “Enter your Name”; N$ INPUT “enter radius of a circle”; R

FOR i = 1 TO 10 PRINT “circumference is” CIR(R)

NV$ = yz$(N$) END FUNCTION

PRINT N FUNCTION CIR(B)

NEXT P=3.14

END CIR=2*PI*A

FUNCTION xyz$ (y$) FUNCTION

xyz$ = c$

END FUNCTION

c. DECLARE FUNCTION CHECK(N)

CLS d. DECLARE FUNCTION series (i)

INPUT “Enter any number”; N CLS
IF A=ARM THEN

A=N FOR i = 1 TO 5
ARM=CHECK (T) PRINT series(n);
PRINT “Armstrong number”

ELSE END
PRINT “Not Armstrong number”

END IF NEXT

END FUNCTION series (i)
FUNCTION CHECK (N)

S=5 s=i^3

WHILE N<>0 S = series
R=N MOD 10

S=S+R^3 FUNCTION END
N=N\10

END

S = CHECK

END FUNCTION

New Gateway to Computer Science Book -10 225

e. DECLARE FUNCTION FACT(N)

CLS

INPUT “Enter any number” N

END

PRINT “The factorial is” FACT (N)

FUNCTION FACT (N)

F=10

F=F*I

NEXT I

FOR I=1 TO N

FACT=F

END FUNCTION
5. Study the following programs and answer the given questions.
a. DECLARE FUNCTION AB(n)
CLS
INPUT “Enter any number”; x

PRINT “Result=”; AB(x)
END
FUNCTION AB(n)

WHILE n<>0
r= n mod 10
s=s*10+r
n=n\10
WEND
AB=s
END FUNCTION
i. What output will be displayed by the computer if you enter 54?
ii. List out the arithmetic operators used in the above program.
b. DECLARE FUNCTION ADD(N)
CLS
FOR CNT= 1 TO 3
READ NUM

226 New Gateway to Computer Science Book -10

PRINT ADD(NUM) 227
NEXT CNT
DATA 8, 7, 3
END
FUNCTION ADD(N)
S=0
FOR G= 1 TO N
S=S+G
NEXT G
ADD=S
END FUNCTION
i. Name the actual and formal parameter used in the above program.
ii. What is the value of NUM when the value of CNT is 2?
c. DECLARE FUNCTION Num (N)
INPUT N
S = Num (N)
PRINT S
END
FUNCTION Num (N)
x = int 917/N)
Y = 15 MOD N
Num = X + Y
END FUNCTION
i. Write the name of the function used in the above program.
ii. List out the mathematical function (Library) used in the above program.
d. DECLARE FUNCTION COUNT (A$)
Input “Enter a word”; w$
END
FUNCTION Count (A$)
B = LEN (A$)
C$ = UCASE$ (A$)
FOR = 1 TO B
E$ = MID$ (C$ I, 1)
IF E$ = “A” OR E$ = “E” OR E$ = ‘i’

New Gateway to Computer Science Book -10

OR E$ = “O” OR E$ = “U” THEN
C=C+1
END IF
NEXT I
Count = C
END FUNCTION
i. List the string library functions used in the above program.
ii. Write down the missing statement in the main module to execute the program.
e. DECLARE FUNCTION NUM(N)
CLS
N=14
PRINT NUM(N)
END
FUNCTION NUM(N)
WHILE N<>0
R=N MOD 2
S=S+R*10^P
P=P+1
N=N\2
WEND
NUM=S
END FUNCTION
i. List the local variable used in the function procedure.
ii. What will be the output of the program?
iii. Name of operators used in the above program.
f. DECLARE FUNCTION JPT (N)

FOR ctr = 1 TO 6
READ N
J = JPT (N)
Sum = Sum + J
NEXT ctr
PRINT Sum
DATA 10, 20, 30, 40, 50, 60
END

228 New Gateway to Computer Science Book -10

FUNCTION JPT (N)
IF N MOD 2 = 0 THEN JPT = N
END FUNCTION
i) What is name of the function used in the above program?
ii) How many times will the function be executed in the above program?
g. DECLARE FUNCTION Diff(A,B)
CLS
INPUT “Enter first number”; A
INPUT “Enter second number”; B
PRINT “The difference of the two number=”;Diff(A,B)
END
FUNCTION Diff(A,B)
D=A-B
Diff=D
END FUNCTION
i) What will be the output of the program if the user enters 200 as the first number
and 100 as the second number?
ii) Will the program run if the first line (i.e. DECLARE…..) is deleted?
h. DECLARE FUNCTION Prod(A,B)
CLS
INPUT “Enter first number:”;A
INPUT “Enter second number:”;B
PRINT “the product of two number=”;Prod(A,B)
END
FUNCTION Prod(A,B)

P=A*B
Prod=P
END FUNCTION
i) List all the numerical variables used in the program.
ii) List all the local variables used in the program.
i. DECLARE FUNCTION count(N$)
Input “Enter a word”; R$
C= Count(R$)
Print C

New Gateway to Computer Science Book -10 229

END
FUNCTION count(N$)
For k=1 to LEN(n$)
X$=MID$(N$,K,1)
IF UCASE$(X$)=”A” then
X=X+1
End if
Next K
Count = X
END FUNCTION
i) List any two library functions used in the above program.
ii) Write the use of variable ‘C’ in line 3 [i.e. C=Count(R$)] given above.
j. DECLARE FUNCTION check(a)
CLS
FOR i=1 TO 4
READ n
IF check(n) = 1 THEN PRINT n
NEXT i
DATA 256, 432, 125, 361
FUNCTION check(n)
s=SQR(n)
IF s=INT(s) THEN
check = 1
ELSE
check = 0
END IF
END FUNCTION
i. What is the output of the above program?
ii. Does the program give the same output if INT() function is replaced by FIX()

function?
iii. If FOR I = 1 to 4 is changed as FOR i=1 to 5, then will the program run? Why?
iv. Replace the FOR…NEXT loop with WHILE….WEND loop.

230 New Gateway to Computer Science Book -10

k. DECLARE FUNCTION NUM (N)
CLS
N = 14
PRINT NUM(N)
END
FUNCTION NUM (N)
WHILE N <> 0
R = N MOD 2
S = S + R * 10 ^ P
P=P+1
N=N\2
WEND
NUM = S
END FUNCTION
i. List out the local variables used in the above program.
ii. What will be the value of S if the value of N = 10?
iii. What will be the output of the above program?
iv. List out the name of the operators used in the above program.

New Gateway to Computer Science Book -10 231

Lesson

10 File Handling in QBASIC

Learning Outcomes

At the end of this lesson, students will be able to:
 tell about file handling.
 explain program file and data file.
 describe the types of files.
 use file handling statements with syntax.
 work with file modes in Qbasic.

File is a collection data or information. Almost all information stored in a computer must
be in a file. In our daily life, we create different types of files such as paint file, word pad
file, excel file, text file, etc. We also save our QBASIC programs with .BAS file extension
that is also a type of file. So, we can say that everything in computer is stored in a file. File
handling is a process to create data file, write data to the data file and read data from it. It
is simply a writing program and storing information in files for future use which helps in
managing data and working with data, we can search data from files, delete it, and so on.
The file of the computer can be categorized into two types which are explained below.

Program File

Program file is a set of instructions written in a computer language for data processing
under unique file name. Program files are associated with providing access and execution
of a program on the computer system. A program file extension is added to a file name by
the program itself. It contains instructions which are used to manipulate data and display
the output. The program files extension of Qbasic is .BAS is extension.

Data File

Data file are a collection of data such as name, address, phone, etc. that are required for
data processing. In data file, related data are stored in a row known as a record. Data file
are stored separately from the program file. It provides data to the program files. Data
files are linked to a program file during the execution. Let’s discuss some advantages of
data files.

232 New Gateway to Computer Science Book -10

 The data stored in data file can be reused as required.
 Single data file can be used by different programs.
 Data file provides flexibility to recover saved data whenever required.
 The data files can be created, modified or deleted as required.

Types of Files

Sequential Access Files

Sequential data file is a file that must be accessed in a sequential order. Data from these
files are read sequentially, item after item, starting at the beginning. Sequential access
file is easy to handle. In the sequential file, we cannot change the existing entry or insert
a new entry. To do this, we should copy the entire content to another file making the
changes on the way and then copy the correct data back in to the original file. We cannot
simply locate or go back to read a particular data item. A disk consists of a number of
concentric circles known as tracks. The data items are stored on these tracks sequentially.

Random Access Files

Random access means the ability to read and write anywhere in the file, where data are
simply appended to the end of the file and is accessed by crossing from the start of the
file in sequential order. Random access files are created on disks that allow us to read or
write from the files in random order. Random access files allow more flexible access than
the sequential files. You can read or write any record directly in a random file without
searching through all the records.

Statements and their Functions with Syntax

OPEN statement: This statement is used to open sequential data file for writing data to the
data file or reading data from the data file using Output, Input, or Append mode.

Syntax: OPEN “FILENAME.DAT” FOR <MODE> AS #FILE NUMBER

WRITE # statement: This statement is used to send or store one or more data items to
the specified file using either output or append mode. It inserts commas between the data
items and it encloses strings in double quotation marks.

Syntax: WRITE # File number, Variable list

PRINT # statement: This statement is used to add spaces between data items while storing
data and it does not enclose strings in double quotation marks.

New Gateway to Computer Science Book -10 233

Syntax: PRINT # Variable list
CLOSE # statement: This statement is used to close only specific files.
Syntax: CLOSE #File number
INPUT # statement: This statement is used to read data of the existing sequential data file
and stores them in a variable.
Syntax: INPUT # File number, variable list
LINE INPUT # Statement: This statement reads the entire line or maximum 255 characters
from the keyboard or sequential file.
Syntax: LINE INPUT # File number, Variable List
EOF () function: This function is used to check the end of file marker while reading the
data from the open sequential file. It returns true if the end of file has been reached and
returns FALSE when the end of file has not been reached.
Syntax: EOF(File Number)
INPUT $ () Statement: This statement returns a specified number of characters from a
data file.
Syntax: INPUT$ (N,# File number)
FILES statement: This statement displays the list of file names of a current directory.
Syntax: FILES(Specs)
NAME AS statement: This statement is used to rename a file.
Syntax: NAME “old name” AS “new name”.
KILL statement: This statement is used to delete a file from the specified disk or directory.
Syntax: KILL “File name”
MKDIR statement: This statement is used to create new directory in a specified location
to manage a file.
Syntax: MKDIR “path”
CHDIR statement: This statement is used to change the default directory on a specified
drive location.
Syntax: CHDIR “location name”
RMDIR statement: This statement is used to delete a directory from a disk. It can remove
only empty directory.
Syntax: RMDIR “Path Name”

234 New Gateway to Computer Science Book -10

File Modes in QBASIC

The purpose of opening data files is called file mode. The Common modes of opening
data files are listed below.

OUTPUT: In file handling this mode is used to create a new sequential file and writes data
into it. It starts writing data always from the beginning of the file.

Syntax: OPEN “filename” FOR file mode AS # file number

Where, file name refers to the name of file that is to open, file mode refers to output,
input or append modes of opening file, file number refers to a reference number to the file
opened.

Example: OPEN “file.dat” FOR OUTPUT AS #2orOPEN “O”, #2, “file.dat”

It opens the file file.dat file in output mode the file is referenced to as file number #2. Here,
the user creates a new file file.dat for the purpose of writing data into the file.

INPUT: In file handling this mode is used to read / display data from the existing data
files.

Example: OPEN “file.dat” FOR INPUT AS #2 or OPEN “I”, #2, “file.dat”

APPEND: In file handling this mode is used to add more records in the existing data file.
It starts adding data after the last record of the previous file.

Example: OPEN “file.dat” FOR APPEND AS #2 or OPEN “A”, #2, “file.dat”

Creating a Sequential File

Creating a file has three stages that we are going to discussed below.
a. Creating a file for output by using a statement such as:
Open “file.dat” FOR OUTPUT AS #1
b. Store the data
This is typically achieved by accepting input from the keyboard and then writing it to
thefileusingaWRITE#1statement.Thestatementsthatarewrittentothefilemustinclude
the correct channel number. An example of how to store a single record is included
below.
INPUT “Enter the name”; Name$
INPUT “Enter the address” Add$

New Gateway to Computer Science Book -10 235

INPUT “Enter the telephone number”; T
‘ Write data to the file ‘
WRITE #1, Name$, Add$,T

This block of statement will only store one record. If you want to store more records
you need to put these statements inside a loop.

c. Close the file

Sequential files need to be closed when you have finished with them. If you forget
to close it, you will be unable to open the file. To perform this operation you can use the
statement as given below.

CLOSE#1

Example 1

Write a program to store only one record of an employee including his / her employee ID,
name, post and department in data file EMP.DAT
OPEN “EMP.DAT” FOR OUTPUT AS #1
CLS
INPUT “ENTER ID”;I
INPUT “ENTER NAME”; N$
INPUT “ENTER POST”; P$
INPUT “ENTER DEPARTMENT”;D$
WRITE #1,I, N$, P$, D$
CLOSE #1
Example 2

Write a program to store the records of employee including their Employee ID, name,
post and department in a data file EMP.DAT
OPEN “EMP.DAT” FOR OUTPUT AS #2
TOP:
CLS
INPUT “ENTER ID”;I
INPUT “ENTER NAME”; N$
INPUT “ENTER POST”; P$
INPUT “ENTER DEPARTMENT”;D$

236 New Gateway to Computer Science Book -10

WRITE #2,I,N$, P$, D$
INPUT “DO YOU WANT TO CONTINUE? Y/N”;CH$
IF UCASE$(CH$)=“Y” THEN GOTO TOP
CLOSE #1
Example 3

Write a program to store the records of any 10 customers along with their name, address,
product name and price in a sequential data file “product.txt”.
OPEN “product.txt” FOR OUTPUT AS #2
FOR I = 1 TO 10
CLS
INPUT “ENTER CUSTOMER’S NAME”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER PRODUCT’S NAME”; P$
INPUT “ENTER PRICE” ; P
WRITE #1, N$,A$,P$,P
NEXT I
CLOSE #2
END

Reading a Sequential File

Data read from a sequential file is read in order starting from the beginning of the file. You
should make sure that the statement read the file, read the fields in the same order, they
were written to the file and you stopped reading when the end of file reached. Reading a
sequential file consists of the following three stages.

a. Open the file for reading

OPEN “file.dat” FOR INPUT AS#1

b. Read records from the file

INPUT #1, Name$, Add$, T = Will read an entire records and store the results in

string variables.

PRINT “Name:” Name$ = Print out values to screen in an appropriate format.

PRINT “Address”; Add$

PRINT “Telephone Number”; T

New Gateway to Computer Science Book -10 237

You need to place these statements inside a loop. But how do you know when it is time to
finish reading? You can test for End-of-File using EOF function as below.
DO UNTIL EOF(1)
‘ Read a record
‘ Print it
LOOP
c. When all processing has been done you need to close the file as given below.
CLOSE#1

Example 1

Write a program to display all the records of data file “STUDENT.DAT”.
CLS
OPEN “STUDENT.DAT” FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, N$,R,C,S
PRINT “NAME”,“ROLL NO”,“CLASS”,“SECTION”
PRINT N$,R,C,S
LOOP
CLOSE #1
END
Example 2

Write a program to display those records whose salary is greater than 15000 from the data
file “STAFF.DAT”.
OPEN “STAFF.DAT” FOR INPUT AS #1
DO WHILE NOT (EOF)
INPUT #1,N$,ADD$,A,S
IF S>=15000 THEN
PRINT N$,ADD$,A,S
ENDIF
LOOP
CLOSE #1
END

238 New Gateway to Computer Science Book -10

Example 3
Write a program to display all the records from “rec.dat”. Also count how many records
have been displayed.
OPEN “REC.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
LINE INPUT #1,A$
C=C+1
WEND
CLOSE#1
PRINT “NO. OF RECORDS DISPLAYED=”;C
END

Adding Records to a Sequential File

Sometimes, we need to go back to a file and add records to the end of it. This is done in a
similar manner to creating a file and adding records. But in this case the file is opened in
a different mode.

OPEN filename FOR APPEND AS #n

Here, the APPEND keyword indicates that any output is going to the end; rather than the
beginning of a file. In #n, n indicates the file number.

Appending records to the end of file consist of the following stages.

a. Open the file

OPEN “file.dat” FOR APPEND AS #1

b. Writing records to the file

INPUT “Enter the name”; N$

INPUT “Enter the address”; Add$

INPUT “Enter telephone number”; T

WRITE #1, N$, Add$, T

c. Closing the file when you have finished

CLOSE #1

New Gateway to Computer Science Book -10 239

Example 1

Write a program to add some data in an existing sequential data file “emp.dat” having
field name, post and salary.
OPEN “EMP.DAT” FOR APPEND AS #1
CLS
DO
INPUT “ENTER NAME” ; N$
INPUT “ENTER POST” ;P$
INPUT “ENTER SALARY” ; S
WRITE #1, N$,P$,S
INPUT “ADD MORE DATA ? (Y/N)” ; C$
CH$=UCASE$(C$)
LOOP WHILE UCASE(CH$)=“Y”
CLOSE#1
END

Example

A sequential data file “STUDENT.DAT” contains few records under the fields name, roll
no and marks for three subjects. WAP to add few more records into the same data file.
OPEN “STUDENT.DAT” FOR APPEND AS #1
TOP :
CLS
INPUT “ENTER ROLL NUMBER”;RN
INPUT “ENTER NAME”; N$
INPUT “ENTER MARKS OF ANY THREE SUBJECTS”;X,Y,Z
WRITE #1,RN,N$,X,Y,Z
INPUT “DO YOU WANT TO CONTINUE?”;CH$
IF UCASE$(CH$) = “Y” THEN GOTO TOP
CLOSE #1
END

240 New Gateway to Computer Science Book -10

Searching for Records

If you are interested in certain records, you have to start at the beginning of the file and
read a record at a time in order. You can read the file of a record at a time and only print
out those records which meet your requirements.
Example 1

WAP to search record according to the supplied name from the data file “RESULT.DAT”
where fields are student’s name, class and marks secured in three subjects.
OPEN “RESULT.DAT” FOR INPUT AS #1
TOP:
CLS
FLAG = 0
INPUT “ENTER NAME TO SEARCH”;S$
WHILE NOT EOF(1)
INPUT #1,N$,C,X,Y,Z
IF UCASE$(S$)=UCASE$(N$) THEN
FLAG =1
PRINT N$,C,X,Y,Z
END IF
WEND
IF FLAG = 0 THEN PRINT “DATA NOT FOUND”
INPUT “DO YOU WANT TO RETRY?”; CH$
IF UCASE$(CH$)= “Y” THEN GOTO TOP
CLOSE #1
END

New Gateway to Computer Science Book -10 241

Practical Section on File Handling

Program 1

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 “RESULT.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter name”; N$
INPUT “Enter address”; A$
INPUT “Enter marks in three different subjects”; M1, M2, M3
WRITE #1, N$, A$, M1, M2, M3
INPUT “Do you want to continue(Y/N)”;CH$
LOOP WHILE UCASE$(CH$)=“Y”
CLOSE #1
END

Program 2

Create a sequential data file “std.dat” to store name and marks obtained in English, Maths
and Science for a few students.
OPEN “STD.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter name”; N$
INPUT “Enter marks in English”; E
INPUT “Enter marks in Maths”; M
INPUT “Enter marks in Science”; S
WRITE #1, N$, E, M, S
INPUT “Do you want to continue(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) =“Y”
CLOSE #1
END

242 New Gateway to Computer Science Book -10

Program 3

Write a program to create a data file “result.dat” to store name, address and obtained
Marks in three different subjects of students.
OPEN “RESULT.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter name”; N$
INPUT “Enter address”; A$
INPUT “Enter marks in three different subjects”; M1, M2, M3
WRITE #1, N$, A$, M1, M2, M3
INPUT “Do you want to continue(Y/N)”; CH$
LOOP WHILE UCASE$(CH$)=“Y”
CLOSE #1
END

Program 4

Create a sequential data file “HOTEL.DAT” to store customer’s name, address, and phone
number. Program should terminate with the user’s choice.
OPEN “HOTEL.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter customers name”; CN$
INPUT “Enter address”; A$
INPUT “Enter phone number”; P#
WRITE #1, CN$, A$, P#
INPUT “Do you want to continue(Y/N)”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END

New Gateway to Computer Science Book -10 243

Program 5

WAP to create sequential file “Person.DAT” containing information like name, age, DOB
and sex. Add records in this data file as per the users choice.
OPEN “PERSON.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter name”; N$
INPUT “Enter age”; A$
INPUT “Enter date of birth”; D$
INPUT “Enter Sex”; S$
WRITE #1, N$, A$, D$, S$
INPUT “Do you want to continue(Y/N)”; CH$
LOOP WHILE UCASE$(CH$)=“Y”
CLOSE #1
END

Program 6

Create a sequential data file “Price.dat” to store item name, quantity and rate. Also
calculate the total amount. Program should terminate according to the user’s choice.
OPEN “PRICE.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “Enter item name”; N$
INPUT “Enter quantity”; Q
INPUT “Enter rate”; R
T= Q*R
WRITE #1, N$, Q, R
INPUT “Do you want to continue(Y/N)”;CH$
LOOP WHILE UCASE$(CH$)=“Y”
CLOSE #1
END

244 New Gateway to Computer Science Book -10

Program 7

Create a sequential data file’post.dat’ to store name and marks of any three subjects. Also
calculate total and percentages only for 15 students.
OPEN “POST.DAT” FOR OUTPUT AS #1
FOR I= 1 TO 15
CLS
INPUT “Enter your name”; N$
INPUT “Enter marks in three different subjects”; M1, M2, M3
T=M1+M2+M3
P= T/3
WRITE #1, N$, M1, M2, M3, T, P
NEXT I
CLOSE #1
END
Program 8

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
Program 9

A sequential data file named “record.dat” contains first name, last name and age. Write a
program to display all the records whose age is more than 60.
OPEN “RECORD.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, FN$, LN$, A

New Gateway to Computer Science Book -10 245

IF A>60 THEN PRINT FN$, LN$, A
WEND
CLOSE #1
END
Program 10

Write a program to open a data file “RECORD.DAT” that contains name, address, date
of birth, e-mail and telephone number of some employees. Now display all those records
whose date of birth is in current month.
OPEN “RECORD.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A$, D$, E$, T#
B$=LEFT$(D$, 2)
C=VAL(B$)
E$=LEFT$(DATE$, 2)
F=VAL(E$)
IF C=F THEN PRINT N$, A$, D$, E$, T#
WEND
CLOSE #1
END

Program 11

Write a program to display all records having salary less than 2500 from the data file
“ADD.INF” having the field’s name, post and salary.
OPEN “ADD.INF” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, P$, S
IF S<2500 THEN PRINT N$, P$, S
WEND
CLOSE #1
END

246 New Gateway to Computer Science Book -10

Program 12

WAP to delete some records from “neps.dat” file where computer ask user to enter the
record, which is to be deleted. (Fields are name, address, and telephone number).
OPEN “NEPS.DAT” FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS #1
CLS
INPUT “Enter name which is to be deleted”; D$
WHILE NOT EOF(1)
INPUT #1, N$, A$, T#
IF UCASE$(D$)<>UCASE$(N$) THEN
WRITE #2, N$, A$, T#
ELSE
PRINT “Deleted data=”; N$, A$, T#
END IF
WEND
CLOSE #1, #2
KILL “NEPS.DAT”
NAME “TEMP.DAT” AS “NEPS.DAT”
END

Program 13

Write a program to open a data file “STUDENT.DAT” that contains name, address,
telephone number and parent’s name of some students. Now display all those records
whose address is “LALITPUR”.
OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A$, T#, PN$
IF UCASE$(A$) =“LALITPUR” THEN PRINT N$, A$, T#, PN$
WEND
CLOSE #1
END

New Gateway to Computer Science Book -10 247

Program 14

Write a program to search and display only those record whose percentage is more than
60% from the data “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$, T, P, DA$
IF P>60 THEN PRINT SN, D$, T, P, DA$
WEND
CLOSE #1
END
Program 15

A sequential data file “SALARY.DAT” contains the information, employee code,
employees name, post, and basic salary. Write a program to display those records whose
basic salary is in between 10000 to 15000 and post is ‘OFFICER’
OPEN “SALARY.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, EC, EN$, P$, S
IF B$>=10000 AND B$<=15000 AND UCASE$(P$)= “OFFICER” THEN
PRINT EC, EN$, P$, S
END IF
WEND
CLOSE #1
END
Program 16

A sequential data file ‘student.dat’ contains ten students name, roll, house, address. Write
a program to read and display last five student’s record.
OPEN “SALARY.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1,n$,r,h$,ad$

248 New Gateway to Computer Science Book -10

FOR I = 1 TO 10
IF I >5 THEN
PRINTn$,r,h$,ad$
NEXT i
WEND
CLOSE#1
END
Program 17

A data file named “Staff.dat” contains staff name, department, post and salary of some
staff. Write a program to count and display total number of records in a file.
OPEN “STAFF.DAT” FOR INPUT AS #1
CLS
C=0
WHILE NOT EOF(1)
INPUT #1, N$, D$, P$, S
C=C+1
WEND
PRINT “Total number of records=”; C
CLOSE #1
END
Program 18

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, N$, A, T, R
I=(A*T*R)/100
PRINT N$, A, T, R, I
WEND
CLOSE #1
END

New Gateway to Computer Science Book -10 249

Program 19

WAP that asks a post of the employee and displays his/her records from the sequential
data file “XYZ.REC” having fields name, post, dept and salary.
OPEN “XYZ.REC” FOR INPUT AS #1
CLS
INPUT “Enter post to be searched”; S$
FLAG=0
WHILE NOT EOF(1)
INPUT #1, N$, P$, D$, S
IF UCASE$(S$)=UCASE$(P$) THEN
PRINT N$, P$, D$, S
FLAG=1
END IF
WEND
IF FLAG=0 THEN PRINT “Data not found”
CLOSE #1
END
Program 20

WAP to search record according to the supplied name from the data file “RESULT.DAT”
where fields are student’s name, class and marks secured in three subjects.
OPEN “RESULT.DAT” FOR INPUT AS #1
TOP:

CLS
FLAG = 0
INPUT “ENTER NAME TO SEARCH”;S$
WHILE NOT EOF(1)
INPUT #1,N$,C,X,Y,Z
IF UCASE$(S$)=UCASE$(N$) THEN
FLAG =1
PRINT N$,C,X,Y,Z
END IF
WEND
IF FLAG = 0 THEN PRINT “DATA NOT FOUND”

250 New Gateway to Computer Science Book -10


Click to View FlipBook Version