Set-1
a) Write a program to calculate and print the simple interest using FUNCTION……END FUNCTION.
Ans: DECLARE FUNCTION INTEREST (P,T,R)
CLS
INPUT “Enter principal, time and rate”; P,T,R
PRINT “The simple interest is”; INTEREST(P,T,R)
END
FUNCTION INTEREST(P,T,R)
I=(P*T*R)/100
INTEREST=I
END FUNCTION
b) Write a program to print the natural numbers from 1 to 5 using SUB…END SUB.
Ans: DECLARE SUB SERIES ( )
CLS
CALL SERIES
END
SUB SERIES
FOR I = 1 TO 5
PRINT I
NEXT I
END SUB
c) 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.
Ans: 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
Set-2
a) Write a program in QBASIC to find the total surface area of a box using FUNCTION….END FUNCTION.
Ans: DECLARE FUNCTION TSA(L,B,H)
CLS
INPUT “Enter length, breadth and height”; L,B,H
PRINT “The total surface area of box=”; TSA(L,B,H)
END
FUNCTION TSA(L,B,H)
T=2*(L*B+B*H+L*H)
TSA=T
END FUNCTION
b) Write a program to input three different numbers in the main module thenfind the greatest number using SUB….END SUB.
Ans: DECLARE SUB GREAT(A,B,C)
CLS
INPUT “Enter any three numbers”; A,B,C
CALL GREAT(A,B,C)
END
SUB GREAT(A,B,C)
IF A>B AND A>C THEB
PRINT A;”is greatest number”
ELSEIF B>A AND B>C THEN
PRINT B;”is greatest number”
ELSE
PRINT C;”is greatest number”
END IF
END SUB
c) 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.
Ans: 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
Set-3
a) Write a program using FUNCTION….END FUNCTION to get a word from the user and then print the word in reverse order
Ans: DECLARE FUNCTION REV$(S$)
CLS
INPUT “Enter any word”; S$
PRINT “The reverse word is”; REV$(S$)
END
FUNCTION REV$(S$)
FOR I = LEN(S$) TO 1 STEP -1
B$=MID$(S$,I,1)
W$=W$+B$
NEXT I
REV$=W$
END FUNCTION
b) Write a program using SUB….END SUB to get radius of circle and then print its circumference.
Ans: DECLARE SUB CIRC(R)
CLS
CONST PI=3.1416
INPUT ”Enter radius”; R
CALL CIRC(R)
END
SUB CIRC(R)
C=2*PI*R
PRINT “The circumference of circle=”;C
END SUB
c) 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.
Ans: 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
Set-4
a) Write a program using FUNCTION…..END FUNCTION to find the average of any two numbers given by the user.
Ans: DECALRE FUNCTION AVE(A,B)
CLS
INPUT “Enter any two numbers”; A,B
PRINT “The average of two numbers=”; AVE(A,B)
END
FUNCTION AVE(A,B)
AV=(A+B)/2
AVE=AV
END FUNCTION
b) Write a program using SUB……END SUB to check whether the number given by the user is positive or negative.
Ans: DECLARE SUB CHECK(N)
CLS
INPUT ”Enter any number”; N
CALL CHECK(N)
END
SUB CHECK(N)
IF N>0 THEN
PRINT N;”is positive number”
ELSEIF N<0 THEN
PRINT N;”is negative number”
END IF
END SUB
c) 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.
Ans: 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
Set-5
a) Write a program using FUNCTION….END FUNCTION to get a word from the user and print the word in the reverse order.
Ans: DECLARE FUNCTION REV$(S$)
CLS
INPUT “Enter any word”; S$
PRINT “The reverse word is”; REV$(S$)
END
FUNCTION REV$(S$)
FOR I = LEN(S$) TO 1 STEP -1
B$=MID$(S$, I, 1)
W$=W$+B$
NEXT I
REV$=W$
END FUNCTION
b) Write a program using SUB….END SUB to get radius of the circle and display the area.
Ans: DECLARE SUB AREA(R)
CLS
CONST PI=3.1416
INPUT ”Enter radius”; R
CALL AREA(R)
END
SUB AREA(R)
A=PI*R^2
PRINT “The area of circle”; A
END SUB
c) A sequential data file called “Marks.dat” contains roll number, name, English, Nepali, and Mathsfield.Write a program to display all the content of the data file.
Ans: 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
Set-6
a) Write a program using Function…..End Function to get temperature in Celsius from the user and then print the temperature in Fahrenheit.(hint: F=9C/5+32)
Ans: DECLARE FUNCTION CONVERT (C)
CLS
INPUT “ENTER TEMPERATURE IN CELCIUS”; C
PRINT “TEMPERATURE IN FARENHEIT=”; CONVERT (C)
END
FUNCTION CONVERT (C)
F = 9 * C / 5 + 32
CONVERT = F
END FUNCTION
b) Write a program using Sub….End Sub to get a word from the user and then print it in reverse order.
Ans: DECLARE SUB REV (S$)
CLS
INPUT "ENTER ANY STRING"; S$
CALL REV(S$)
END
SUB REV (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
PRINT "REVERSED STRING IS "; W$
END SUB
c) 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.
Ans: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
Set-7
a) Write a program to find the numbers of vowels in an input string using ‘FUNCITON…..END FUNCTION’.
Ans: DECLARE FUNCTION COUNT (S$)
CLS
INPUT "ENTER ANY STRING"; S$
PRINT "TOTAL NO. OF VOWELS= "; COUNT(S$)
END
FUNCTION COUNT (S$)
VC = 0
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
VC = VC + 1
END IF
NEXT I
COUNT = VC
END FUNCTION
b) Write a program using sub procedure module to print the series 1,1,2,3,5,8.. up to ten terms.
Ans: DECLARE SUB SERIES ( )
CLS
CALL SERIES
END
SUB SERIES ( )
A = 1
B = 1
FOR I = 1 TO 10
PRINT A;
C = A + B
A = B
B = C
NEXT I
END SUB
c) 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.
Ans: 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
Set-8
a) Write a program to print the following series: 1,4,7,……. up to 10th term using FUNCTION……END FUNCTION.
Ans: DECLARE FUNCTION SERIES
CLS
D= SERIES
END
FUNCTION SERIES
FOR I = 1 TO 10
PRINT A;
A=A+3
NEXT I
END FUNCTION
b) Write a program to display the series 55555, 5555, 555, 55, 5 using SUB………END SUB.
Ans:DECLARE SUB SERIES()
CLS
CALL SERIES
END
SUB SERIES
A#= 55555
FOR I = 1TO 5
PRINT A#;
A#=A#\10
NEXT I
END SUB
c) A data file named “RECORD.DAT” contains name, age and salary for ‘n’ number of persons. Write a program to input a name to search data from data file. If the data is not found, then display the message “Data not found in the list.”
Ans:OPEN “RECORD.DAT” FOR INPUT AS #1
CLS
INPUT “Enter name to search data”; S$
FLAG=0
WHILE NOT EOF(1)
INPUT #1, N$, A, S
IF UCASE$(S$)=UCASE$(N$) THEN
PRINT N$, A, S
FLAG=1
END IF
WEND
IF FLAG=0 THEN PRINT “DATA NOT FOUND”
CLOSE #1
END
No comments:
Post a Comment