Review of QBASIC Statements
Fill in the banks:
Computer cannot work without program.
Computer programmers are responsible for designing, writing and modifying computers programs.
QBASIC is an example of high level language.
Errors in a computer program are called bug.
Algorithms and flowcharts are two important tools to design a solution.
Two types of errors in computer programs are syntax and logical.
The % symbol at the end of a variable name declared integer variable.
A variable name should not contain blank space or keyword.
Different types of expressions supported by QBASIC are string, numeric and logical.
AND operator is an example of logical operator.
State whether the following statements are true or false:
An expression may be combination of variables, constants and operators. TRUE
OR operator returns ‘-1’ or ‘True’ value only if all conditions are true. FALSE
Data is an executable statement. FALSE
The value of a symbolic constant can be changed during the execution of a program. FALSE
IF statement is a decision-making statement. TRUE
WHILE -WEND is a post test -looping statement. FALSE
Write down the syntax and purposes of the following statements and functions:
INPUT
Syntax:
INPUT [“Input prompt”][;|,] Variable1,Variable2…….
Purpose: Execution pauses, a user can enter data from keyboard and execution continues.
PRINT
Syntax: PRINT [#File number,], [Expression] Separator [Expression]............
Purpose: The PRINT statement writes data on the screen or in the data file.
DIM (Declare in Memory)
Syntax: DIM Var1 [Subscript] [As data type] , Var1 [Subscript] [As data type]
Purpose: It is used to declarare simple variables or array variables with their data types at the
Beginning of a program.
READ-DATA
Syntax: READ Variable1,Variable2…..
DATA item1,Item2…….
Purpose: When it is necessary to supply a fixed list of data then READ-DATA statements are
Suitable.
SQR( )
Syntax: SQR(x) , where x is a numeric expression that is greater than or equal to zero.
Purpose: It is used to return the square root of any positive number.
MID$( )
Syntax: MID$(String expression, start, length)
Purpose: It is used to return of a specific number of characters from string.
FOR - NEXT
Syntax: FOR Counter = start value to final value[ step increment]
The block of statements that will be executed once for each iteration of loop
NEXT [counter]
Purpose: It is used to execute a series of instruction a given number of times.
DO - LOOP
Syntax: DO WHILE CONDITION
Statements Block
LOOP
Purpose: It repeats the block of statements while the condition is true or until the condition becomes
true.
IF - END IF
Syntax: IF (Condition)
Statement
END IF
Purpose: In the above case if the condition is true, the statement given next to THEN will be
executed. If the condition is false, next executable statement following the IF... THEN
statement will be executed.
SELECT - END SELECT
Syntax:
SELECT CASE Test- expression
case expression list
statements Block 1
case expression list
statements Block 2
case expression list
statements block 3
[ CASE ELSE
statements block 4]
END SELECT
Purpose: It is a control flow statements that executes one of the several Statement block
depending on the value of an expiration, which may be string or numeric type.
EXIT
Syntax: EXIT {DO | FOR | FUNCTION | SUB}
Purpose: EXITS from a DO or FOR loop, a function or SUB procedure.
Write algorithms and draw flowcharts for the following:
To find the greatest number among three different numbers
ANSWER:
Algorithm:
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b and a>c then
Display a is the largest number.
If b>a and b>c then
Display b is the largest number.
Otherwise,
Display c is the greatest number.
Step 5: Stop
Flowchart
To Find the shortest string among 10 different strings
ANSWER: Algorithm:
Step 1: Start
Step 2: Declare variables a,b as string
Step 3: Initialize variable
I←1
Step 4: Read variable a
Step 5: Repeat the steps until i<=10
5.1 If length of b is less than a then
a=b
5.2 i←i+1
Step 6: Display a
Step 7: Stop
Flowchart
To print and find the sum of the first ten multiples of 5.
ANSWER:
Step 1: Start
Step 2: Declare variables n,i,sum
Step 3: Initialize variables
n←5
I←1
sum←0
Step 4: Repeat the steps until i<=10
4.1 n←n*i
Display n
Sum←sum+i
4.2 i←i+1
Step 5: Display sum
Step 6: Stop
Flowchart
To check whether the input number is divisible by 5 and 7 or not
ANSWER:
Algorithm:
Step 1: Start
Step 2: Declare variables n,r
Step 3: Read variable n
Step 4: If remainder of n÷5 and n÷7 equals 0
Display n is divisible by 5 and 7.
Else
Display n is not divisible by 5 and 7
Step 5: Stop
Flowchart:
To check whether the input number is prime or composite
ANSWER:
Algorithm:
Step 1: Start
Step 2: Read “n” value to check prime or composite
Step 3: Set I=1, Count=0
Step 4: Check I<=n if true goto step 5, else goto step 8
Step 5: If remainder of n÷i equals 0 then evaluate step 6
if not goto step 7
Step 6: Set Count=count+1
Step 7: I=I+1 goto step 4
Step 8: Check count, if count =2 display n as prime,
If not display n as composite.
Flowchart for Prime or Composite
Debug the following programs:
INPUT “Enter the percentage of a student”;P
IF R>=60 THEN
D$= “First Division:
ELSE R>=50 OR R<=59.9 THEN
D$= “Second Division”
ELSE R>=40 and R<=49.9 THEN
D$= “Third Division”
ELSE
D$= “Fail”
END
PRINT D$
END
ANSWER:
INPUT “Enter the percentage of a student”;R
IF R>=60 THEN
D$= “First Division”
ELSEIF R>=50 AND R<=59.9 THEN
D$= “Second Division”
ELSEIF R>=40 and R<=49.9 THEN
D$= “Third Division”
ELSE
D$= “Fail”
END IF
PRINT D$
END
REM to Print the first 10 even numbers from 50 to 100 and find their sum
L=1
N=100
S=0
DO
PRIT N
S=S-N
N=N+2
L=L-1
WHILE L<=20
PRINT “SUM=”;N
END
ANSWER:
REM to Print the first 10 even numbers from 50 to 100 and find their sum
L=1
N=50
S=0
DO
PRIT N
S=S+N
N=N+2
L=L+1
WHILE L<=20
PRINT “SUM=”;S
END
REM example of EXIT statement
N=1
DO
PRINT N
S=S+N
N=N-1
IF N=11 THEN EXIT LOOP
LOOP
PRINT “SUM=”;SUM
END
ANSWER:
REM example of EXIT statement
N=1
DO
PRINT N
S=S+N
N=N+1
IF N=11 THEN EXIT DO
LOOP
PRINT “SUM=”;S
END
Rewrite the following programs using DO-LOOP WHILE statement.
REM LOOP with fractional number
FOR J=10 to 50.5 step 1.5
PRINT J,
NEXT J
END
ANSWER4:
REM LOOP with fractional number
CLS
J=10
DO
PRINT J,
J=J+1.5
LOOP WHILE J<=50.5
END
REM skipped loop
FOR N = 10 TO 1 STEP-1
PRINT N^2
NEXT N
END
ANSWER: REM skipped loop
CLS
N=10
DO
PRINT N^2
N=N-1
LOOP WHILE N > = 1
END
A=1
B=1
L=1
CLS
PRINT “Loop begins”
DO UNTIL L>10
PRINT A
C=A+B
A=B
B=C
L=L+1
LOOP
PRINT “Loop Stops”
END
ANSWER:
A=1
B=1
L=1
CLS
PRINT “Loop begins”
DO
PRINT A
C=A+B
A=B
B=C
L=L+1
LOOP WHILE L<=10
PRINT “Loop Stops”
END
Type the following programs, see the output and answer the given questions.
CLS
S=0
L=1
WHILE L<=10
READ N
PRINT N
S=S+N
L=L+1
WEND
A=S/10
PRINT A
DATA 3,4,5,2,3,6,7,8,5,6
END
Dry Run:
A = S/10 = 49/10 = 4.9
Output: 3 4 5 2 3 6 7 8 5 6
4.9
i) List the variables and operators used in the above program.
ANSWER: VARIABLES S , L , A
OPERATORS < = (Relational Operator) + , / (Arithmetic Operator)
ii) What happens if you remove “L=L+1” line form the program.
ANSWER: If “L=L+1” is removed then out of DATA error message will appear on screen.
iii) Rewrite the program using FOR-NEXT statement.
ANSWER: CLS
S=0
FOR I = 1 TO 10
READ N
PRINT N
S=S+N
NEXT I
A=S/10
PRINT A
DATA 3,4,5,2,3,6,7,8,5,6
END
FOR K = 1 TO 5
FOR J = 1 TO K
PRINT J,J+K
NEXT J
NEXT K
END
Dry Run:
OUTPUT:
1 2
1 3
2 4
1 4
2 5
3 6
1 5
2 6
3 7
4 8
1 6
2 7
3 8
4 9
5 10
i) Rewrite the above program using WHILE-WEND.
ANSWER:
CLS
K=1
WHILE K<=5
J=1
WHILE J<=K
PRINT J,J+K
J=J+1
WEND
K=K+1
WEND
END
CLS
FOR J=1 TO 5
Z=0
FOR A = 1 TO 2
Z=Z+J*A
NEXT A
PRINT Z
NEXT J
END
Dry Run:
OUTPUT:
3
6
9
12
Rewrite the above program using DO-LOOP WHILE statement.
ANSWER:
CLS
J=1
DO
Z=0
A=1
DO
Z=Z+J*A
A=A+1
LOOP WHILE A<=2
PRINT Z
J=J+1
LOOP WHILE J<=5
END
Write the programs for the following:
Write a program to find the area of a triangle where the area of triangle = ½x(bxh).
ANSWER:
CLS
INPUT “Enter the base and height of triangle” , B,H
A=1/2*(B*H)
PRINT “Area of triangle is”,A
END
Write a program to find the simple interest where SI=PTR/100.
ANSWER:
CLS
INPUT “Enter the principal, time and rate” , P,T,R
SI=(P*T*R)/100
PRINT “Simple Interest is”,SI
END
Write a program to calculate the volume of a cube where V=L3.
ANSWER:
CLS
INPUT “Enter the length of cube” , L
V=L^3
PRINT “Volume of cube is”,V
END
Write the programs to print the following series:
1,3,5,7,....upto 10th terms
ANSWER:
CLS
P=1
FOR I = 1 TO 10
PRINT P
P=P+2
NEXT I
END
100,95,90…… upto 20th terms
ANSWER:
CLS
P=100
FOR I = 1 TO 20
PRINT P
P=P-5
NEXT I
END
1,4,9…...upto 10th terms
ANSWER:
CLS
FOR P = 1 TO 10
PRINT P^2
NEXT P
END
2,2,4,6,10 …. Upto 20th terms
ANSWER:
CLS
A=2
B=2
FOR I = 1 TO 20
PRINT A
C=A+B
A=B
B=C
NEXT I
END
During expo Nepal, Pure Leather Collection offers the following rate of discount on purchases:
Purchases Amount Discount
<1000 Nil
>=1000 7%
>=3500 9%
>=5000 15%
Write a program to input the purchase amount, which should be greater than Rs.100 and print the discount amount and the price to be paid.
ANSWER:
CLS
top:
INPUT “Enter the purchase amount”,PA
IF PA<=100 then goto top:
IF PA<1000 THEN
D=0
ELSEIF PA>=1000 AND PA<3500 THEN
D=(PA*7)/100
ELSEIF PA>=3500 AND PA<5000 THEN
D=(PA*9)/100
ELSE
D=(PA*15)/100
END IF
PAY=PA-D
PRINT “Discount Amount is ”;D
PRINT “ Payable amount after discount is”;PAY
END
Write a program to print all prime numbers between 1 to 100.
ANSWER:
CLS
FOR N = 1 TO 100
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN PRINT N,
NEXT N
END
Write a program to input a sentence and print it in the title case.
ANSWER:
CLS
INPUT "Enter a sentence"; s$
b$ = UCASE$(MID$(s$, 1, 1))
w$ = w$ + b$
FOR i = 2 TO LEN(s$)
IF RIGHT$(w$, 1) = " " THEN
b$ = UCASE$(MID$(s$, i, 1))
ELSE
b$ = LCASE$(MID$(s$, i, 1))
END IF
w$ = w$ + b$
NEXT i
PRINT w$
END
Write a program to store the system date of your computer to a string variable and print it in given format: YYYY / MM / DD. (default date in QBASIC is MM-DD-YYYY)
ANSWER:
CLS
P$=DATE$
MONTH$=LEFT$(P$,2)
DAY$=MID$(P$,4,2)
YEAR$=RIGHT$(P$,4)
PRINT YEAR$; “/”; MONTH$ ; “/”;DAY$
END
Write a program to print all Armstrong numbers , which come between 100 to 999.
ANSWER:
CLS
FOR P = 100 TO 999
A=0
B=P
WHILE B< >0
R= B MOD 10
A=A+R^3
B=B\10
WEND
IF A = P THEN PRINT P
NEXT P
END
Write a program to print the following output using nested loop:
55555
4444
333
22
1
ANSWER:
CLS
FOR P = 5 TO 1 STEP-1
FOR L= 1 TO P
PRINT P;
NEXT L
NEXT P
END
123456
12345
1234
123
12
1
ANSWER:
CLS
FOR P = 6 TO 1 STEP-1
FOR L = 1 TO P
PRINT L;
NEXT L
NEXT P
END
54321
5432
543
54
5
ANSWER:
CLS
FOR P = 1 TO 5
FOR L = 5 TO P STEP-1
PRINT L;
NEXT L
NEXT P
END
Write a program to input 15 different numbers in an array variable and print their sum.
ANSWER:
CLS
DIM N(15)
FOR P = 1 TO 15
INPUT “Enter the number”; N(P)
SUM=SUM+N(P)
NEXT P
PRINT “Sum of input numbers is”;SUM
END
Write a program to input 15 different numbers using an array variable. Print only the even numbers and find their sum.
ANSWER:
CLS
DIM N(15)
FOR P = 1 TO 15
INPUT “Enter the number”;N(P)
NEXT P
FOR L= 1 TO 15
IF N(L) MOD 2 = 0 THEN PRINT N(L)
SUM=SUM+N(L)
NEXT I
PRINT “Sum of even numbers is”;SUM
END
Write a program to input 10 different numbers in an array variable and print them in the ascending order.
ANSWER:
CLS
DIM N(10)
FOR P = 1 TO 10
INPUT “Enter the number”;N(P)
NEXT P
FOR I = 1 TO 15
FOR J = 1 TO 15-I
IF N(J) > N(J+1) THEN
SWAP N(J), N(J+1)
END IF
NEXT J
NEXT I
PRINT “Numbers in Ascending order”
FOR P = 1 TO 15
PRINT N(P)
NEXT P
END
Write a program to input 10 different numbers in an array variable and print them in the descending order.
ANSWER:
CLS
DIM N(10) AS STRING
FOR P = 1 TO 10
INPUT “Enter the number”;N(P)
NEXT P
FOR I = 1 TO 10
FOR J = 1 TO 10-I
IF N(J) < N(J+1) THEN
SWAP N(J), N(J+1)
END IF
NEXT J
NEXT I
PRINT “Numbers in Descending order”
FOR P = 1 TO 15
PRINT N(P)
NEXT P
END
Write a program to input a string and print it in reverse form.
ANSWER:
CLS
INPUT “Enter a string”;W$
FOR I = LEN(W$) TO 1 STEP-1
P$=P$+ MID$(W$,I,1)
NEXT I
PRINT “String in reverse order is ”;P$
END
Write a program to input 15 numbers in an array then print only even numbers.
ANSWER:
CLS
DIM N(15)
FOR P = 1 TO 15
INPUT “Enter the number”;N(P)
NEXT P
FOR L= 1 TO 15
IF N(L) MOD 2 = 0 THEN PRINT N(L)
NEXT L
END
***** END OF CHAPTER - 9 *****