Friday, October 21, 2016

SOLVED QBASIC SET K AND L

 DOWNLOADED FROM SUSHILUPRETI.COM.NP 

1a) Write a program asking any 3 numbers from the user and find out the middle number.

b) Write down the output of the following program.
CLS
N = 36
FOR J = 1 TO N - 1
IF N MOD J <> 0 THEN
PRINT J;
END IF
NEXT J
END


c) Re-write the following program correcting the bugs:
            CLS
REM "TO PRINT THE SUPPLIED NOS. IN REVERSE ORDER
FOR I = 1 TO 5
READ A
NEXT I
FOR J = 5 TO 1
PRINT A
NEXT A
DATA 78, 88, 87, 77, 7
END



d) Write a program to count the no. of positive integers, negative integers and zeros from the given nos.:
            (17, 22, 0, -11, 11, -77, 0, -1, 2-, 19)

e) WAP that asks Name, Address, Sex, and Roll No. of students and save them in a sequential data file STUDENT.DATA along with a prompt for a user to continue.

f) WAP that asks your name and stores its reverse form into a sequential data file “REVERSE.DAT”. Make a provision so that user can input 5 records at each program execution.

Answers SET K
1a) Program to display the middle number
            DECLARE SUB MIDDLE(A, B, C)
CLS
INPUT “ENTER ANY THREE NUMBERS”; A, B, C
CALL MIDDLE (A, B, C)
END
SUB MIDDLE (A, B, C)
IF A > B AND A < C OR A < B AND A > C THEN
PRINT A; “IS MIDDLE NUMBER”
ELSEIF B > A AND B < C OR B < A AND B > C THEN
PRINT B; “IS MIDDLE NUMBER”
ELSE
PRINT C; “IS MIDDLE NUMBER”
END IF : END SUB
b) Output
            5   7   8   10   11   13   14   15   16    17   19   20   21   22   23   24   25   26   27   28  
            29   30   31   32   33   34   35 

           
c) Debugged Program


            CLS
REM "TO PRINT THE SUPPLIED NOS. IN REVERSE ORDER
FOR I = 1 TO 5
READ A(I)
NEXT I
FOR J = 5 TO 1 STEP -1
PRINT A(J)
NEXT J
DATA 78, 88, 87, 77, 7
END


d) Write a program to count the no. of positive integers, negative integers and zeros from the given nos.:
            (17, 22, 0, -11, 11, -77, 0, -1, 20, 19)
            CLS
FOR I = 1 TO 10
READ N
IF N > 0 THEN
PC = PC + 1
ELSEIF N < 0 THEN
NC = NC + 1
ELSE
OC = OC + 1
END IF
NEXT I

DATA 17, 22, 0, -11, 11, -77, 0, -1, 20, 19
PRINT "TOTAL NUMBER OF POSITIVE INTEGERS="; PC
PRINT "TOTAL NUMBER OF NEGATIVE INTEGERS="; NC
PRINT "TOTAL NUMBER OF ZEROES="; OC


END


e) WAP that asks Name, Address, Sex, and Roll No. of students and save them in a sequential data file STUDENT.DATA along with a prompt for a user to continue.

OPEN “STUDENT.DATA” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER  NUMBER”; N$
INPUT “ENTER ADDRESS”; A$
INPUT “ENTER SEX”; S$
INPUT “ENTER ROLL NUMBER”; R
WRITE #1, N$, A$, S$, R
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END



f) WAP that asks your name and stores its reverse form into a sequential data file “REVERSE.DAT”. Make a provision so that user can input 5 records at each program execution.

OPEN "D:\REVERSE.DAT" FOR OUTPUT AS #1
CLS
FOR A = 1 TO 5
B$ = ""
INPUT "ENTER NAME"; N$
FOR I = LEN(N$) TO 1 STEP -1
B$ = B$ + MID$(N$, I, 1)
NEXT I
WRITE #1, B$
NEXT A
CLOSE #1
END







***

 DOWNLOADED FROM SUSHILUPRETI.COM.NP


1a) What is accumulator? Illustrate with an example.

b) Write down the output of the following program.
CLS
FOR C = 1 TO 4
READ N
PRINT USING "**###.##"; N
NEXT C
DATA 326.326, 123487.2, 4375, 12383, 53.5
END


c) Re-write the following program correcting the bugs:
            CLS
REM PROGRAM TO FIND SUM OF ASCII CODES OF
REM CHARACTERS OF A WORD
INPUT "A WORD:"; W$
S = ZERO
FOR N = 1 TO LEN(W$)
CH$ = MID$(W$, N, 1)
S = S + ASCII(CH$)
LOOP N
PRINT "SUM:"; S
END


d) Write a program to declare a user-defined function that returns simple interest using FUNCTION…..END FUNCTION statement. The program should input principal amount, time and rate in  main module and call the defined function to calculate simple interest.

e) WAP to create a sequential data file RECORD.DAT that stores consumer number, consumer name, last month’s water meter reading and current month reading. The program should allow the user to input records as long as the user wishes.

f) Create a user defined function TRIM$(n$) that removes the leading and trailing spaces from the supplied string without using LTRIM$ and RTRIM$ functions and use it in any suitable program.

Answers SET L
1a) A variable that keeps accumulating (getting larger) until a certain goal is accomplished is known as accumulator. For example: S=S+A. Here S is a accumulator.

           
b) Output
            **326.33
            %123487.20
            *4375.00
            12383.00

           

           
c) Debugged Program
CLS
REM PROGRAM TO FIND SUM OF ASCII CODES OF
REM CHARACTERS OF A WORD
INPUT "A WORD:"; W$
S = 0
FOR N = 1 TO LEN(W$)
CH$ = MID$(W$, N, 1)
S = S + ASC(CH$)
NEXT N
PRINT "SUM:"; S
END

d) Write a program to declare a user-defined function that returns simple interest using FUNCTION…..END FUNCTION statement. The program should input principal amount, time and rate in  main module and call the defined function to calculate simple interest.

            DECLARE FUNCTION INTEREST (P, T, R)
CLS
INPUT “ENTER PRINCIPAL AMOUNT”; P
INPUT “ENTER TIME”; T
INPUT “ENTER RATE”;R
PRINT “SIMPLE INTEREST=”; INTEREST(P, T, R)
END

FUNCTION INTEREST (P, T, R)
I = P* T * R / 100
INTEREST = I
END FUNCTION

e) WAP to create a sequential data file RECORD.DAT that stores consumer number, consumer name, last month’s water meter reading and current month reading. The program should allow the user to input records as long as the user wishes.
OPEN “RECORD.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER CONSUMER NUMBER”; CN
INPUT “ENTER CONSUMER NAME”; N$
INPUT “ENTER LAST MONTH’S WATER METER READING”; L
INPUT “ENTER CURRENT MONTH’S WATER METER READING”; C
WRITE #1, CN, N$, L, C
INPUT “DO YOU WANT TO CONTINUE”; CH$
LOOP WHILE UCASE$(CH$)=”Y”
CLOSE #1
END




f) Create a user defined function TRIM$(n$) that removes the leading and trailing spaces from the supplied string without using LTRIM$ and RTRIM$ functions and use it in any suitable program.


DECLARE FUNCTION TRIM$ (N$)
CLS
INPUT "Enter any string"; N$
PRINT TRIM$(N$)
END

FUNCTION TRIM$ (N$)
B$ = MID$(N$, 1, 1)
FOR I = 2 TO LEN(N$)
C$ = MID$(N$, I, 1)
IF C$ <> SPACE$(0) AND MID$(N$, I - 1, 1) <> SPACE$(0) THEN B$ = B$ + C$
IF C$ <> SPACE$(0) AND MID$(N$, I - 1, 1) = SPACE$(0) THEN B$ = B$ + SPACE$(0) + C$
NEXT I
TRIM$ = B$
END FUNCTION




***

No comments:

Post a Comment