(X) Ans of Ch-11 Exercise QBASIC Function

 Chapter-11

Review of Built -in Functions in QBASIC

  1. Fill in the banks:

  1. Functions manipulate some data and return a value.

  2. Function defined by the user is User defined function called function.

  3. Functions provided by QBASIC system are called Built in or library functions.

  4. Val function is used to convert a number in string form into numeric form.

  5. The ASC functions returns ASCII code  of a character.


  1. State whether each of the following statements are true or false.

  1. ASC function is used to find the ASCII code of any character. TRUE

  2. Functions do not save our time while writing a program. FALSE

  3. The SQR functions processes only positive numbers. TRUE

  4. LEN is a user defined function. FALSE

  5. STR$ function converts the number in the string form into the numeric form. TRUE    


  1. Write the purpose and syntax of the following:

  1. LEN( )

Syntax:     LEN( String Expression)

Purpose:  It returns the length of  number of characters in a string.

  1. MID$( )

Syntax: MID$( String Expression, start, Length)     

Purpose: It returns a specific number of characters from a string.  


  1. LEFT$( )

Syntax:  LEFT$ ( String expression , n)   

Purpose: It is used to extract the specified number of characters beginning from the left most character. 

  1. VAL( )

Syntax: VAL (x$) where, x$ is any string expression.     

Purpose:  It is used to convert a string expression consisting of digits into a numeric value.





  1. INT( )

Syntax: INT (x) , where x is any numeric expression.     

Purpose: It is used to round the numbers to their integer values. In QBASIC, INT usually rounds the number down. 

  1. ABS( )

Syntax: ABS (x) where, x may be any numeric expression.       

Purpose: it is used to obtain the absolute value of a numeric expression.

  1. STR$( )

Syntax:     STR$( x) where, x is any numeric expression.

Purpose:  It is used to convert a numeric expression to its string representation.

  1. SQR( )

Syntax:     SQR(x) where, x is a numeric expression that is greater than or equal to zero.

Purpose:  It returns the square root of any positive number.


  1. Differentiate the following:

  1. STR$( ) and VAL( )


STR$( )

VAL( )

Syntax:     STR$( x) where, x is any numeric expression.

Syntax: VAL (x$) where, x$ is any string expression.     

Purpose:  It is used to convert a numeric expression to its string representation.

Purpose:  It is used to convert a string expression consisting of digits into a numeric value.


  1. ASC( ) and CHR$( )


ASC ( )

CHR$ ( )

Syntax:  ASC (String Expression)

Syntax: CHR$(n)    

Purpose:  It is used to finds ASCII code of a character.

Purpose:  It is used to retrieve the single character represented by the ASCII number.


  1. RIGHT$( ) and MID$( )


RIGHT$ ( )

MID$ ( )

Syntax: RIGHT$ ( String expression , n) 

Syntax: 

MID$(String Expression, start, length) 

Purpose:  It is used to extracts the specific number of characters beginning from the rightmost character of the string.

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


  1. Answer the following questions:


  1. What do you mean by function? Explain with examples.

ANSWER: A readymade program in QBASIC, which take some data ,manipulate them and return a value is called Function.

  1. Differentiate between library and user defined function.

ANSWER: 

Library Function

User Defined Function

1. The function which is provided by QBASIC to do the specific task is called Library or Built in Function.

1. The function which is written and stored by the programmer to do the specific task is called User Defined Function.

2. We can not modify library function.

2.  We can modify user defined   function.


  1. Explain numeric and string functions with some examples.

ANSWER: The function which are used to process numeric data and solve problems, are called numeric function.

Example of numeric functions : ABS Function, INT Function, COS Function etc.

The functions which are used to manipulate strings is called string functions.

Example of String Functions: LEFT$, MID$, LEN Funcitons etc.



  1. “Functions make our work easier”. Explain.

ANSWER: Functions in QBASIC are ready-made programs, which take some data, manipulate them and return a value, which can be a string or a numeric type. Once a function is written and debugged, it can be used again and again. It also reduces the code of a program. In this way , function make our work easier.


  1. Write the output of the following programs:


N$= “KATHMANDU”

           FOR K = 1 TO 5

              PRINT  LEFT$ ( N$ , K )

           NEXT K

           END

ANSWER: Dry Run:


K

LEFT$(N$,K)

1

K

2

KA

3

KAT

4

KATH

5

KATHM



Output:

K

KA

KAT

KATH

KATHM







FOR A =  97 TO 123

PRINT A; “=”; CHR$(A)

NEXT A

END

ANSWER: Output:

97=a

98=b

99=c

100=d

101=e

102=f

103=g

104=h

105=i

106=j

107=k

108=l

109=m

110=n

111=o

112=p

113=q

114=r

115=s

116=t

117=u

118=v

119=w

120=x

121=y

122=z

123={






N$ = “HACKER”

FOR X = 1 TO LEN(N$)

C$ = MID$ (N$ , X ,1)

S = S + ASC(C$)

NEXT X

PRINT S

END

ANSWER: Dry Run:


X

MID$(N$,X,1)

S=S+ASC(C$)

1

H

S=0+72=72

2

A

S=72+65=137

3

C

S=137+67=204

4

K

S=204+75=279

5

E

S=279+69=348

6

R

S=348+82=430



  1. Debug the following programs and write in the correct forms:

  1. Rem To print string in pattern

X = NEPAL

FOR K = 5 TO 1

PRINT RIGHT$ (X$ , I)

NEXT I

END

ANSWER:

X$ = “ NEPAL”

FOR I = 5 TO 1 STEP-1

PRINT RIGHT$ (X$ , I)

NEXT I

END


  1. REM To print string in reverse form

INPUT “Enter a string ” ; N$

L = LEN$ (N$)

FOR K = L TO 1

C$ = MID (N$ , K ,1)

W$ = C$ + W$

NEXT K

PRINT “Reversed String is” ; W$

END 

ANSWER:

REM To print string in reverse form

INPUT “Enter a string ” ; N$

L = LEN (N$)

FOR K = L TO 1 STEP-1

C$ = MID$(N$ , K ,1)

W$ = C$ + W$

NEXT K

PRINT “Reversed String is” ; W$

END



  1. REM to count vowels in a sentence

INPUT “ Enter  string ::::::”;S

L = LEN$(S)

FOR K = 1 TO L

C$=MID(S$,1,K)

SELECT CASE C$

CASE A,E,I,O,U

COUNT + 1 = COUNT

SELECT END

NEXT 

PRINT “Supplied sentence” ; S$

PRINT “Total vowels::::::”; C

END

ANSWER:

REM to count vowels in a sentence

INPUT “ Enter  string ::::::”;S$

L = LEN(S$)

FOR K = 1 TO L

C$=MID$(S$,K,1)

SELECT CASE C$

CASE “A”, “E”, “I”, “O”, “U”

COUNT = COUNT + 1

END SELECT

NEXT K

PRINT “Supplied sentence” ; S$

PRINT “Total vowels::::::”; COUNT

END






  1. Write the programs to print the following patterns:

  1. L

AL

PAL

EPAL

NEPAL

ANSWER:

CLS

P$= “NEPAL”

FOR I = 1 TO LEN(P$)

PRINT RIGHT$(P$,I)

NEXT I

END


  1. *NEPAL*

*NEPA*

*NEP*

*NE*

*N*

ANSWER:

CLS

P$= “NEPAL”

FOR I =  LEN(P$) TO 1 STEP-1

PRINT “*” ; LEFT$(P$,I) ; “*”

NEXT I

END

  1. NEPAL

   EPAL

      PAL

        AL

           L

ANSWER:

CLS

P$= “NEPAL”

A = 1

FOR I =  LEN(P$) TO 1 STEP - 1

PRINT TAB(A) ; RIGHT$(P$,I)

A = A + 1

NEXT I

END

  1. KATHMANDU

   ATHMAND

      THMAN

         HMA

            M

ANSWER:

CLS

P$= “KATHMANDU”

A = 1

B = 9

FOR I =  1 TO 5

PRINT TAB(A) ; MID$ ( P$ , I , B )

A = A + 1

B = B - 2

NEXT I

END



  1. M

HMA

THMAN

ATHMAND

KATHMANDU

ANSWER:

CLS

P$= “KATHMANDU”

A=1

FOR I = 5 TO 1 STEP - 1

PRINT MID$ ( P$ , I , A )

A = A + 2

NEXT I

END










  1. *

***

*****

*******

*********


ANSWER:

CLS

FOR P =  1 TO  9 STEP 2

FOR L = 1 TO P

PRINT “ * ” ;

NEXT L

PRINT

NEXT P

END


  1.         *NEPAL*

      **NEPAL**

    ***NEPAL***

  ****NEPAL****

*****NEPAL*****


ANSWER:

CLS

P$= “NEPAL”

T = 5 

FOR I =  1 TO 5

A$ = STRING$ ( I , 42)

PRINT TAB ( T ) ; A$ ;  P$ ; A$

T = T - 1

NEXT I

END




  1. 1

121

12321

1234321

123454321


ANSWER:

CLS

N# = 1

FOR P = 1 TO 5

PRINT N# ^ 2

N# = N# * 10 + 1

NEXT  P

END


  1. 123454321

1234321

12321

121

1

ANSWER:

CLS

N# = 11111

FOR P = 1 TO 5

PRINT N# * N# 

N# = N# \ 10

NEXT  P

END






  1. Write the programs to solve the following problems:

  1. Write a program to supply a sentence and count the number of consonants in the sentence.

ANSWER:

CLS

INPUT “Enter any sentence” ; P$

FOR I = 1 TO LEN(P$)

A$= UCASE$ ( MID$ ( P$ , I , 1 ) )

SELECT CASE A$

CASE “A”, “E” , “I” , “O” , “U”

CASE “ ”

CASE ELSE

COUNT = COUNT + 1

END SELECT

PRINT “Total number of consonants in the sentence is ” ; COUNT

END

  1. Write a program to supply a word. Then count the total number of vowels.

ANSWER:

CLS

INPUT “Enter any  word ” ; P$

FOR I = 1 TO LEN(P$)

A$= UCASE$ ( MID$ ( P$ , I , 1 ) )

SELECT CASE A$

CASE “A”, “E” , “I” , “O” , “U”

COUNT = COUNT + 1

END SELECT

PRINT “Total number of vowels in supplied word is ” ; COUNT

END






  1. Write a program to supply a sentence and count vowels (A,E,I,O,U) separately.

ANSWER:

CLS

INPUT “Enter any sentence” , S$

FOR I = 1 TO LEN(S$)

P$= UCASE$ ( MID$ ( S$ , I, 1 ) )

IF P$ = “A” OR P$ = “E” OR  P$ = “I” OR  P$ = “U” THEN

COUNT = COUNT + 1 

END IF

NEXT I

PRINT “Total number of vowels ”, COUNT

END

  1. Write a program to supply a character and print whether the character is a letter or not.

ANSWER:

CLS

INPUT “ Enter a character ”, C$

CH$ = UCASE$ ( CH$ )

A = ASC ( CH$ )

IF A > = 65 AND A < = 90 THEN

PRINT “ The supplied character is alphabet.”

ELSE

PRINT “ The supplied character is not alphabet.”

END IF

END








  1. Write a program to read a sentence from the keyboard and print it in title case. (Suppose, Input is , HE IS HARI, output should be , He Is Hari.)

ANSWER:

CLS

INPUT “Enter any 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 “ Sentence in Title Case” ; W$

END


  1. Write a program to supply the first, middle and the last name of a person and print the output as given below.

If input is RAM KUMAR SHARMA output should be R.K.S.

ANSWER:

CLS

INPUT “Enter first , middle and the last name” , N$

B$ = LEFT$ (N$,1)

FOR J = 1 TO LEN(N$)

C$ = MID$(N$,J,1)

IF C$ = “ ” THEN B$ = B$ + “.” + MID$ (N$ , J+1 , 1)

NEXT J

PRINT B$ + “.”

END







  1. Write a program to supply the first, middle and the last name of a person and print the output as given below.

If input is RAJ BAHADUR RAI output should be RAI BAHADUR RAJ.

ANSWER:

CLS

INPUT “Enter First Name”, A$

INPUT “Enter middle Name”, B$

INPUT “Enter Last Name”, C$

PRINT C$ + B$ + A$

END

  1. Write a program to supply a string and print it in a reverse form.

ANSWER:

CLS

INPUT “Enter a string ”, P$

FOR I = LEN(P$) TO 1 STEP-1

B$ = B$ + MID$ ( P$ , I , 1 )

NEXT I

PRINT “String in reverse order is ”, B$

END

  1. Write a program to supply  a word and check whether the supplied word is a palindrome or not. (Palindrome is a word having the same spelling from both sides. Examples: MADAM, MOON are palindromes).

ANSWER:

CLS

INPUT “Enter a string ”, P$

FOR I = LEN(P$) TO 1 STEP-1

B$ = B$ + MID$ ( P$ , I , 1 )

NEXT I

IF P$ = B$ THEN

PRINT P$ ; “ is Palindrome. ”

ELSE

PRINT  P$; “ is not Palindrome. ”

END IF

END





  1. Write a program to print the input word in alternate capital letters. If the input word is NEPAL then output should be NePaL.

ANSWER:

CLS

INPUT “ Enter any string ”, N$
FOR K = 1 TO LEN(N$)

C$=MID$(N$,K,1)

IF K MOD 2 = 1 THEN

W$ = W$ + UCASE$ ( C$)

ELSE

W$ = W$ + LCASE$ (C$)

END IF

NEXT K

PRINT “ String in Alternate capital Letter is  ” ,W$

END

  1. Write a program to print the sum of ASCII codes of each character of input string.

ANSWER:

INPUT “ Enter any string ”, N$ 

FOR P = 1 TO LEN(N$)

C$ = MID$ (N$ , P ,1)

S = S + ASC(C$)

NEXT P

PRINT “ Sum of ASCII codes of each character of input string is ”, S

END

  1. Write a program to read three different strings from the keyboard and print the longest string. (Hint: Use LEN function to find the length)

ANSWER:

CLS

INPUT “Enter three different strings  ”, P$ , L$ , K$

A = LEN(P$)

B = LEN(L$)

C = LEN(K$)

IF A > B AND A > C THEN

G$ = P$

ELSEIF B > A AND B > C THEN

G$ = L$

ELSE

G$ = K$

END IF

PRINT “Longest string is ” , G$

END 

  1. Write a program to input ten different names from the keyboard and print the longest word.

ANSWER:

CLS

DIM N(10) AS STRING

FOR I = 1 TO 10

INPUT N(I)

NEXT I

Longest$=N(1)

FOR J = 2 TO 10

IF LEN(N(J)) > LEN(Longest$) THEN

Longest$= N(J)

END IF

NEXT J

PRINT “ Longest word among 10 different names ”, Longest$

END


  1. Write a program to input 3 names then print the smallest name.

ANSWER:

CLS

INPUT “Enter three different names  ”, P$ , L$ , K$

A = LEN(P$)

B = LEN(L$)

C = LEN(K$)

IF A < B AND A < C THEN

S$ = P$

ELSEIF B < A AND B < C THEN

S$ = L$

ELSE

S$ = K$

END IF

PRINT “Smallest  name  is ” , S$

END 








  1. Write a program to input 3 numbers then print the middle number.

ANSWER:

CLS

INPUT “ Enter any three different numbers ”, A , B , C

IF ( A > B AND A < C ) OR ( A < B AND A > C ) THEN

M = A

ELSEIF ( B > A AND B < C ) OR ( B < A AND B > C ) THEN

M = B

ELSE

M = C

END IF

PRINT “ Middle Number is ”, M

END


***** END OF CHAPTER -11  *****