Friday, October 21, 2016

SOLVED QBASIC SET A AND B



 DOWNLOADED FROM SUSHILUPRETI.COM.NP 

Set A
1.      What is a variable? Mention its types.
2.      Read the above program and answer the following questions:
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
a.       What is the output of the above program?
b.      Does the program give the same output if INT() function is replaced by FIX() function?
c.       If FOR I = 1 to 4 is changed as FOR i=1 to 5, then will the program run? Why?
d.      Replace the FOR…NEXT loop with WHILE….WEND loop.
3.      Write down the output of the following program.
FOR I = 1 TO 10
READ N
B=N MOD 5
IF B=0 THEN C=C+1
NEXT I
DATA 7, 15, 6, 10, 15, 32, 8, 22, 25, 5
PRINT C
END
4.      Re-write the following program correcting the bugs:
REM to print given name 20 times
LET c=1
INPUT “Enter Name;n$
WHILE c less or equal to 20
Print n$
c+1=c
END
5.      Write a program that allows entering a number and check whether the supplied number is positive, negative or zero.
6.      Write a program to calculate the area of a triangle whose three sides a, b, c is given from the keyboard using FUNCTION….END FUNCTION.

Answers:
1.       The data item whose value changes during the program execution is known as variable. Its types are:
i.                     Numeric variable
ii.                   String variable

2.      a. Output:
256
361

b. Yes, the program gives same output if INT() function is replaced by FIX().

c. No, the program will not run if FOR I=1 to 4 is changed as for I= 1 to 5 because during the fifth loop it searches for the value of “n” and “n” will not get any value as only four data values are given. And displays the error message as “Out of DATA”

d.CLS
I=1
WHILE I<=4
READ n
IF CHECK(n) = 1 THEN  PRINT n
I=I+1
WEND
DATA 256, 432, 125, 361

3.      Output
5

4.       REM to print given name 20 times
LET c=1
INPUT “Enter Name;n$
WHILE c <= 20
Print n$
c=c+1
WEND
END
5.      DECLARE SUB CHECK(N)
CLS
INPUT “Enter a number” ;N
CALL CHECK(N)
END

SUB CHECK(N)
IF N>0 THEN
PRINT N; ”is positive”
ELSEIF N<0 THEN
PRINT N; “is negative”
ELSE
PRINT N; “is zero”
END IF
END SUB

6.      DECLARE FUNCTION CAL(A,B,C)
CLS
INPUT “Enter three sides of a triangle” ;A, B, C
PRINT “The area of triangle=”; CAL(A,B,C)
END

FUNCTION CAL(A,B,C)
S=(A+B+C)/2
AR=(S*(S-A)*(S-B)*(S-C))^(1/2)
CAL=AR
END FUNCTION


 
DOWNLOADED FROM SUSHILUPRETI.COM.NP 

Set B
1.      Write down the truth table of AND operator.
2.      Write down the output of the following program:
DIM A(10)
S=0
FOR I = 1 TO 10
READ A(I)
NEXT I
FOR J=10 TO 1 STEP -1
PRINT A(J);
S=S+A(J)
NEXT J
PRINT
PRINT S
DATA 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
END
3.      Re-write the following program correcting the bugs:
DO
OPEN “DATA.DAT” FOR OUTPUT AS 1
INPUT “NAME:”; N$
INPUT “CLASS:”;C$
INPUT “ROLL:”;R$
WRITE #1, N$, C$, R$
INPUT “Add another record/N)?”;and $
LOOP UNTIL UCASE$(ans$)=Y
END
4.      Re-write the following program using WHILE…WEND:
S=0
FOR I = 1 TO 10
X=I^2
S=S+X
NEXT I
PRINT S
END
5.      Write a program to generate following series: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
6.      Write a program using FUNCTION…….END FUNCTION statement to calculate simple interest for a given principle, time and rate of interest.

Answers:
1.      The truth table of AND operator:
CONDITION
OPERATOR
CONDITION
RESULT
TRUE
AND
TRUE
TRUE
TRUE
AND
FALSE
FALSE
FALSE
AND
TRUE
FALSE
FALSE
AND
FALSE
FALSE

2.      OUTPUT
50  45  40  35  30  25  20  15  10  5
275

3.       
OPEN “DATA.DAT” FOR OUTPUT AS #1
DO
INPUT “NAME:”; N$
INPUT “CLASS:”;C
INPUT “ROLL:”;R
WRITE #1, N$, C, R
INPUT “Add another record(Y/N)?”;and$
LOOP UNTIL UCASE$(and$)<>”Y”
CLOSE #1
END


4.      S=0
I=1
WHILE I<=10
X=I^2
S=S+X
I=I+1
WEND
PRINT S
END


5.      DECLARE SUB SERIES()
CLS
CALL SERIES
END

SUB SERIES
A=1
B=1
FOR I = 1 TO 6
PRINT A; B;
A=A+B
B=A+B
NEXT I
END SUB

6.      DECLARE FUNCTION INTEREST(P,T,R)
CLS
INPUT “Enter principle, time and rate”; P, T, R
PRINT  “The simple interest=”;INTEREST(P,T,R)
END

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

No comments:

Post a Comment