(X) Ans of Ch-9 Exercise Review of QBASIC

 Chapter-9

Review of QBASIC Statements

  1. Fill in the banks:

  1. Computer cannot work without program.

  2. Computer programmers are responsible for designing, writing and modifying computers programs.

  3. QBASIC is an example of high level language.

  4. Errors in a computer program are called bug.

  5. Algorithms and flowcharts are two important tools to design a solution.

  6. Two types of errors in computer programs are syntax and logical.

  7. The % symbol at the end of a variable name declared integer variable.

  8. A variable name should not contain blank space or keyword.

  9. Different types of expressions supported by QBASIC are string, numeric and logical.

  10. AND operator is an example of logical operator.


  1. State whether the following statements are true or false:

  1. An expression may be combination of variables, constants and operators. TRUE

  2. OR operator returns ‘-1’ or ‘True’ value only if all conditions are true. FALSE

  3. Data is an executable statement. FALSE

  4. The value of a symbolic constant can be changed during the execution of a program. FALSE

  5. IF statement is a decision-making statement. TRUE

  6. WHILE -WEND is a post test -looping statement. FALSE


  1. Write down the syntax and purposes of the following statements and functions:

  1. INPUT

Syntax:

INPUT [“Input prompt”][;|,] Variable1,Variable2…….

Purpose: Execution pauses, a user can enter data from keyboard and execution continues.

  1. PRINT

Syntax: PRINT [#File number,], [Expression] Separator [Expression]............

Purpose: The PRINT statement writes data on the screen or in the data file.


  1. 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.

  1. 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. 





  1. 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.

  1. MID$( )

Syntax: MID$(String expression, start, length)

Purpose: It is used to return of a specific number of characters from string.

  1. 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.

  1. 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.

  1. 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.


  1. 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.

  1. EXIT

Syntax: EXIT {DO | FOR | FUNCTION | SUB}


Purpose: EXITS from a DO or FOR loop, a function or SUB procedure.



  1. Write algorithms and draw flowcharts for the following:

  1.  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








  1.  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


  1.  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


  1.  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:





  1.  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





  1. Debug the following programs:

  1. 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


  1. 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


  1. 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




  1. Rewrite the following programs using DO-LOOP WHILE statement.

  1. 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







  1. 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



  1. 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:


Step

N

S=S+N

Print N

1

3

0+3=3

3

2

4

3+4=7

4

3

5

7+5=12

5

4

2

12+2=14

2

5

3

14+3=17

3

6

6

17+6=23

6

7

7

23+7=30

7

8

8

30+8=38

8

9

5

38+5=43

5

10

6

43+6=49

6


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

PRINT

NEXT K

END

Dry Run:


K = 1 to 5

J = 1 to K

Print J

Print J+K

1

1

1+1=2


2

1

1

1+2=3

2

2

2+2=4


3

1

1

1+3=4

2

2

2+3=5

3

3

3+3=6



4

1

1

1+4=5

2

2

2+4=6

3

3

3+4=7

4

4

4+4=8





5

1

1

1+5=6

2

2

2+5=7

3

3

3+5=8

4

4

4+5=9

5

5

5+5=10


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

PRINT 

WEND

END

  1. 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:


J

A

Z=Z+J*A

Z

1

1

0+1*1

1

2

1+1*2

3

2

1

0+2*1

2

2

2+2*2

6

3

1

0+3*1

3

2

3+3*2

9

4

1

0+4*1

4

2

4+4*2

12

5

1

0+5*1

5

2

5+5*2

12


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








  1. Write the programs for the following:

  1. 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


  1. 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


  1. 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


  1. Write the programs to print the following series:

  1. 1,3,5,7,....upto 10th terms

ANSWER:

CLS

P=1
FOR I = 1 TO 10

PRINT P

P=P+2

NEXT I

END


  1. 100,95,90…… upto 20th terms

ANSWER:

CLS

P=100
FOR I = 1 TO 20

PRINT P

P=P-5

NEXT I

END



  1. 1,4,9…...upto 10th terms

ANSWER:

CLS
FOR P = 1 TO 10

PRINT P^2

NEXT P

END


  1. 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


  1. 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

  1. 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


  1. 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


  1. 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


  1. 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



  1. Write a program to print the following output using nested loop:

  1. 55555

4444

333

22

1

ANSWER:

CLS

FOR P = 5 TO 1 STEP-1

FOR L= 1 TO P

PRINT P;

NEXT L

PRINT

NEXT P

END


  1. 123456

12345

1234

123

12

1

ANSWER:

CLS

FOR P = 6 TO 1 STEP-1

FOR L = 1 TO P

PRINT L;

NEXT L

PRINT

NEXT P

END


  1. 54321

5432

543

54

5

ANSWER:

CLS

FOR P = 1 TO 5

FOR L = 5 TO P STEP-1

PRINT L;

NEXT L

PRINT 

NEXT P

END


  1. 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



  1. 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







  1. 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


  1. 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







  1. 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



  1. 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 *****