Chapter-11
Review of Built -in Functions in QBASIC
Fill in the banks:
Functions manipulate some data and return a value.
Function defined by the user is User defined function called function.
Functions provided by QBASIC system are called Built in or library functions.
Val function is used to convert a number in string form into numeric form.
The ASC functions returns ASCII code of a character.
State whether each of the following statements are true or false.
ASC function is used to find the ASCII code of any character. TRUE
Functions do not save our time while writing a program. FALSE
The SQR functions processes only positive numbers. TRUE
LEN is a user defined function. FALSE
STR$ function converts the number in the string form into the numeric form. TRUE
Write the purpose and syntax of the following:
LEN( )
Syntax: LEN( String Expression)
Purpose: It returns the length of number of characters in a string.
MID$( )
Syntax: MID$( String Expression, start, Length)
Purpose: It returns a specific number of characters from a string.
LEFT$( )
Syntax: LEFT$ ( String expression , n)
Purpose: It is used to extract the specified number of characters beginning from the left most character.
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.
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.
ABS( )
Syntax: ABS (x) where, x may be any numeric expression.
Purpose: it is used to obtain the absolute value of a numeric expression.
STR$( )
Syntax: STR$( x) where, x is any numeric expression.
Purpose: It is used to convert a numeric expression to its string representation.
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.
Differentiate the following:
STR$( ) and VAL( )
ASC( ) and CHR$( )
RIGHT$( ) and MID$( )
Answer the following questions:
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.
Differentiate between library and user defined function.
ANSWER:
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.
“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.
Write the output of the following programs:
N$= “KATHMANDU”
FOR K = 1 TO 5
PRINT LEFT$ ( N$ , K )
NEXT K
END
ANSWER: Dry Run:
Output:
K
KA
KAT
KATH
KATHM
FOR A = 97 TO 123
PRINT A; “=”; CHR$(A)
NEXT A
END
ANSWER: Output:
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:
Debug the following programs and write in the correct forms:
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
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
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
Write the programs to print the following patterns:
L
AL
PAL
EPAL
NEPAL
ANSWER:
CLS
P$= “NEPAL”
FOR I = 1 TO LEN(P$)
PRINT RIGHT$(P$,I)
NEXT I
END
*NEPAL*
*NEPA*
*NEP*
*NE*
*N*
ANSWER:
CLS
P$= “NEPAL”
FOR I = LEN(P$) TO 1 STEP-1
PRINT “*” ; LEFT$(P$,I) ; “*”
NEXT I
END
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
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
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
*
***
*****
*******
*********
ANSWER:
CLS
FOR P = 1 TO 9 STEP 2
FOR L = 1 TO P
PRINT “ * ” ;
NEXT L
NEXT P
END
*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
121
12321
1234321
123454321
ANSWER:
CLS
N# = 1
FOR P = 1 TO 5
PRINT N# ^ 2
N# = N# * 10 + 1
NEXT P
END
123454321
1234321
12321
121
1
ANSWER:
CLS
N# = 11111
FOR P = 1 TO 5
PRINT N# * N#
N# = N# \ 10
NEXT P
END
Write the programs to solve the following problems:
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 *****