The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Vedanta Let's Log in Computer Science Book - 9.indd

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by PHILOMATH Contextualizing Mathematics, 2021-07-14 10:05:36

Vedanta Let's Log in Computer Science Book - 9.indd

Vedanta Let's Log in Computer Science Book - 9.indd

Vedanta Let’s Log in Computer Science | Book 9

(c) Looping Structure

In computer science, a loop is a programming structure that repeats a
sequence of instructions until a specific condition is met.

Example:
CLS
c=1
top:

PRINT "Namaste"
c=c+1
IF c <= 5 THEN GOTO top
END

Flowchart of the above program is: Memory table of dry run
Start
C =1 c OP (Output)
1 Namaste
Print “Namaste” 2 Namaste
3 Namaste
4 Namaste
5 Namaste
6

c <= 5 ? Yes ? The output of the above
program is
No
Stop Namaste
Namaste
Namaste
Namaste
Namaste

Here the statement PRINT “Namaste” is executed 5 times till the condition c<=5
is true.

251 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

QBASIC standard looping statements:

a) DO … LOOP WHILE statement

Syntax:
DO

[statementblock]
LOOP [WHILE booleanexpression]

booleanexpression is an expression that returns non-zero (true) or
zero (false).
statementblock is any number of statements on one or more lines
which are to be executed as long as booleanexpression is true.
Example:
CLS
c=1
DO
PRINT "Namaste"
c=c+1
LOOP WHILE c <= 5
END

Counters, Intermediate Value, Sentinel Value, and Accumulators

Counters are generally the numeric variables, which store the number of times
a statements or a block of statements is executed in a loop. Counters may have
increment or decrement of any numbers.

The mid-values of a counter except initial and final value in a loop are called
intermediate value and the final value of a counter is called sentinel value.

Accumulators are the variables in which the intermediate value of another variable
is added.

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 252

Vedanta Let’s Log in Computer Science | Book 9

Example:
A=1
DO
PRINT A;
S=S+A
A = A+1
LOOP WHILE A<=10
PRINT “Sum = “;S
END

In the above program, the variable A is used as a counter which counts the
number of execution of a block of statements in the loop. Here, the intermediate
values of the counter A are 2,3,4,5,6,7,8, and 9. Similarly, the sentinel value of A is
10. The variable S is used as an accumulator which adds the intermediate values
of A.

b) DO WHILE … LOOP statement

Syntax:
DO [WHILE booleanexpression]

[statementblock]
LOOP
Example:
CLS
c=1
DO WHILE c <= 5

PRINT "Namaste"
c=c+1
LOOP
END

253 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

c) WHILE …WEND statement

Syntax:
WHILE condition

[statements]
WEND

condition is an expression that returns non-zero (true) or zero (false)
statements are one or more QBASIC statements to be executed as long
as condition is true
Example:
CLS
c=1
WHILE c <= 5

PRINT "Namaste"
c=c+1
WEND
END

d) DO … LOOP UNTIL statement

Syntax:
DO

[statementblock]
LOOP [UNTIL booleanexpression]
Example:
CLS
c=1
DO

PRINT "Namaste"
c=c+1
LOOP UNTIL c>5
END

Note: The loop is continued until the condition is true. That is, if the given Boolean expression
returns false (0), the loop is continued. The loop is terminated if the condition is true.

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 254

Vedanta Let’s Log in Computer Science | Book 9

The above program displays “Namaste” 5 times. When the value of c reaches 6,
the given conditions return true (1) value and the loop is terminated.

e) DO UNTIL …. LOOP statement

Syntax:
DO [UNTIL booleanexpression]

[statementblock]
LOOP
Example:
CLS
c=1
DO UNTIL c>5

PRINT "Namaste"
c=c+1
LOOP
END

f) FOR … NEXT statement

Syntax:
FOR counter = start TO end [STEP increment]

[statements]
NEXT counter

counter is a numeric variable used as the loop counter
start is the initial value and end is the final value of the counter
increment is the amount the counter is incremented each time
through the loop
Example:
CLS
FOR c=1 TO 5 STEP 1
PRINT “Namaste”
NEXT c
END

Note: The above program displays “Namaste” 5 times.

255 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

Here, the counter is c and its initial value is 1 and final value is 5 and after each
iteration, the value of the counter is incremented by 1.
Similarly, the given program also gives the same output.

CLS
FOR c=10 TO 2 STEP -2

PRINT “Namaste”
NEXT c
END

Note: If the counter has to be incremented by 1, you do not need to specify it with
STEP keyword.
For example, the following program displays “I love my country” 5 times.

CLS
FOR x=1 TO 5

PRINT “I love my country”
NEXT x
END

Endless loop

A loop becomes endless loop if it is not terminated and also called a perpetual
loop.

Example:
CLS
c=1
DO

PRINT c;
c=c-1
LOOP WHILE c <= 5
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 256

Vedanta Let’s Log in Computer Science | Book 9

Note: In the above program, the value of counter ‘c’ is decremented by 1 in each
iteration and the condition c<=5 always becomes true. So, the loop is continued for

infinite number of times.

Nested Loop

A loop becomes nested if it contains one or more loops inside it.

Example:

Outer Loop CLS Output
Inner Loop FOR I = 1 TO 5 1234
1234
FOR j = 1 TO 4 1234
PRINT j; 1234
1234
NEXT j
PRINT
NEXT I
END

EXIT FOR statement

Purpose: to execute a control flow statement that exits a FOR...NEXT loop
Syntax:
EXIT FOR
Example:
CLS
FOR c = 1 TO 10

PRINT c;
IF c = 6 THEN EXIT FOR
NEXT c
END

Output: 34 5 6
12

257 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

When the value of c reaches 6, the loop is terminated prematurely although the
final value of the counter c is 10.

Indentation

Indentation is the process of formatting the code in such a way so as to make it
more readable and easy to understand esp. using tabs.

Example:
CLS
INPUT "Type any number "; N
IF N MOD 2 = 0 THEN

PRINT "Even"
ELSE

PRINT "Odd"
END IF
END

In the above program, the two statements PRINT "Even" and PRINT "Odd" are
indented towards right to make it more readable.

Sample Programs

i) To display the numeric pattern 7,77,777,7777,77777

n=7
FOR i = 1 TO 5

PRINT n;
n = n * 10 + 7
NEXT i
END

ii) To check whether the supplied number is prime

CLS
INPUT "Type any number "; n

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 258

Vedanta Let’s Log in Computer Science | Book 9

FOR x = 1 TO n
IF n MOD x = 0 THEN c = c + 1

NEXT x
IF c = 2 THEN PRINT "Prime" ELSE PRINT "Composite"
END

OR

CLS
INPUT "Type any number "; N
FOR X = 2 TO N - 1

IF N MOD X = 0 THEN
PRINT "The number is composite."
C=1
EXIT FOR

END IF
NEXT X
IF C <> 1 THEN PRINT "The number is Prime"
END

iii) To calculate the sum of individual digits of an integer

CLS
INPUT "Type any integer "; n%
WHILE n% <> 0

r = n% MOD 10
s% = s% + r
n% = n% \ 10
WEND
PRINT "Sum of individual digits = "; s%
END

259 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

How it works?
Let's assume the value of n% is 3245.

Start

N = 3245

Is Yes
N<> 0
r = n mod 10
? s= s + r
n = n / 10
No
Print S

Stop

iv) To display the greatest number among three different numbers

CLS
INPUT "Any three numbers "; x, y, z
IF x > y AND x > z THEN

PRINT x
ELSEIF y > z AND y > z THEN

PRINT y
ELSE

PRINT z
END IF
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 260

Vedanta Let’s Log in Computer Science | Book 9

v) To convert binary to decimal

CLS
INPUT "Type any binary number "; b
WHILE b <> 0

r = b MOD 10
d=d+r*2^p
b = b \ 10
p=p+1
WEND
PRINT "Decimal Equivalent is "; d
END

How it works?
Let's assume the value of b is 1100.

Iteration (cycle) of loop r db p
1 0 0 110 1
2 0 0 11 2
3 1 41 3
4 1 12 0 4

In the fourth iteration, the value of b becomes 0 and the loop is terminated.
The output is:
Decimal Equivalent is 12

POINTS TO REMEMBER

Control structures are used to control the flow of program. Three types of control
structure are: Sequential, Selection, and Iteration.

In sequential structure, all the statements are executed from top to bottom
sequentially without leaving anyone.

261 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

In selection structure (also called branching), the control of the program is transferred
from one part of the program to another on the basis of specified condition or without
condition.
In looping structure (also called iteration), statements are re-executed for a definite
number of times.

Exercise

1. Answer the following questions:
a) What is control structure? Mention its types.
b) Define branching statement with its types.
c) What is unconditional branching?
d) What is conditional branching?
e) What is looping? List the standard looping statements of QBASIC.
f) What is endless loop? Write any program with an endless loop.
g) Define nested loop with a suitable example.

2. Re-write the following program using:
a) DO WHILE … LOOP
b) DO … LOOP UNTIL
c) FOR … NEXT

i) CLS
c = 10
top:
PRINT c;
c=c-1
IF c > 1 THEN GOTO top
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 262

Vedanta Let’s Log in Computer Science | Book 9

ii) CLS
n=1
top:
PRINT “Hello World!”;
n=n+1
IF n <= 5 THEN GOTO top
END

3. Re-write the following program using:
a) DO … LOOP WHILE
b) DO UNTIL … LOOP

i) CLS
FOR A = 5 TO 1 STEP -1
FOR B = 1 TO A
PRINT B;
NEXT B
PRINT
NEXT A
END

ii) CLS
FOR x = 5 TO 1 STEP -1
FOR y = 5 TO x STEP -1
PRINT y;
NEXT y
PRINT
NEXT x
END

263 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

4. Debug the following programs:

a) REM to find the smaller number among two different numbers
CLS
INPUT "Type any two numbers "; x, y
IF x > y THEN PRINT x ELSE PRINT y
END IF
END

b) REM to check odd or even
CLS
INPUT "Type any number "; a
R=a/2
IF R = 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
END

c) REM to display 5,10,15,20,....80
CLS
n=5
DO
PRINT n;
n=n+5
LOOP UNTIL n <= 80
END

d) REM to display 20,18,16,14,....2
CLS
FOR n = 20 TO 2
PRINT n;
n=n+1
NEXT n
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 264

Vedanta Let’s Log in Computer Science | Book 9

e) REM to check prime or composite
CLS
INPUT "Type any number "; n
FOR x = 2 TO n - 1
r= x MOD n
IF r = 0 THEN c = c + 1
NEXT x
IF c = 0 THEN
PRINT "Prime"
ELSE
PRINT "Composite"
END

f) REM to find the greatest among three different numbers
CLS
INPUT "Type any three different numbers "; p, q, r
IF p > q OR p < r THEN
PRINT a
ELSEIF q > p OR q < r THEN
PRINT q
ELSE
PRINT r
END IF
END

g) REM to display the suppled name 10 times
LET c=1
INPUT “Enter Name; N
DO WHILE c less or equal to 10
PRINT N
c+1=c
WEND
END

265 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

5. Write down the output of the following programs:
a) CLS
S=3
FOR Z = 10 TO 5 STEP -1
X=Z^2
S=S+X
NEXT Z
PRINT S
END
b) I = 1
DO
READ N
B = N MOD 4
IF B = 0 THEN C = C + 1
I=I+1
LOOP WHILE I <= 10
DATA 17,16,6,14,8,32,82,22,25,5
PRINT C
END
c) CLS
FOR x = 1 TO 3
READ n$, add$, age
PRINT n$, add$, age
NEXT x
DATA RAM,"4GB",2100
DATA "Harddisk","1TB",5000
DATA PenDrive,32GB,850
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 266

Vedanta Let’s Log in Computer Science | Book 9

d) CLS
n = 15
DO
IF n MOD 3 = 1 THEN PRINT n;
n=n-1
LOOP WHILE n >= 3
END

e) CLS
H=5
FOR I = 2 TO 12 STEP 3
PRINT H;
SWAP H, C
C=H+1
NEXT I
END

f) FOR X = 2 TO 15 STEP 2
IF X >= 11 THEN EXIT FOR
PRINT X ^ 2 + 1

NEXT X
PRINT "Done"
END
g) FOR i=2 TO 10 STEP 2
FOR j=i TO 10 STEP 2
PRINT J;
NEXT j

PRINT
NEXT i
END

267 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

Lab Activities

Group 1 – Sequential Structure

a) Write a program that asks any number and calculates its square, cube, square
root, and cube root.

b) Write a program that accepts the room temperature in Fahrenheit degree and
calculates its equivalent in Celsius degree.

c) Write a program that asks any two numbers and displays the remainder when
the first number is divided by the second one without using MOD operator.

Group 2 – Selection Structure

d) Write a program that asks the age of a person and tells whether he/she can vote.
[Note: A person of 18 years old or above is eligible to vote.]

e) Write a program that asks any number and checks whether it is divisible by 3.

f) Write a program that asks any three numbers and displays the greatest number.

g) Write a program that accepts an alphabet and tells whether it is vowel or
consonant.

h) Write a program that asks the length of three sides of a triangle and checks
whether it is a right angled triangle.

i) Write a program that asks student’s name and marks in English, Nepali and
Computer Science, and calculates the total marks, percentage, result (pass/fail)
and division as the criteria below.

Percentage Division

>=80 Distinction

>=60 and <80 First Division

>=45 and <60 Second Division

>=32 and <45 Third Division

Note: Pass mark for all the subject is 40]

j) Write a program that asks student’s roll numbers and tells him/her room number

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 268

Vedanta Let’s Log in Computer Science | Book 9

where he/she has to sit to take the exam as in the criteria below:

Roll No. Room No.

1-10 5

11-20 6

22,25,29,31 7

Above 35 8

Others 9

k) Write a program that asks for the principal amount and time in years and
calculates the simple interest at the rate given below:

Time in yrs Rate

<2 5%

2 to 4 7%

>4 9

l) Write a program that asks for particulars name, rate and quantity, and calculates
the discount amount based on the following criteria.

Total amount Discount %

Less than Rs. 1000 No discount

Rs 1000 – Rs 5000 5%

Above Rs. 5000 10%

Group 3 – Looping Structure

m) Write a program that asks for your name and displays it 10 times.
n) Write a program to display the first 100 natural numbers with their sum.
o) Write a program to generate the following series:

i) 1,4,7, …, up to 10th terms
ii) 100,95,90,85,…..,5
iii) 1,2,4,8,16,…., up to 10th terms
iv) 1,2,4,7,11,16,22, … , up to 10th terms
v) 1,4,9,16,…, up to 10th terms
vi) 1,8,27,64,…, up to 10th terms

269 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

vii) 999,728,511,342,…, up to 10th terms

viii) 1,11,111,1111,1111
ix) 1,2,3,5,8,13,21,…, up to 10th terms
x) .1,.01,.001,.0001,…, up to 10th terms

p) Write a program that asks for any two integers and calculates their HCF and LCM.

q) Write a program that asks for any numbers and displays its multiple table up to 10.

r) Write a program that asks for any number and checks whether it is Armstrong
or not. [A number which is equal to the sum of the cube of its individual digits is
called an Armstrong number. Eg. 371 = 33+73+13]

s) Write a program that checks whether the supplied number is palindrome or not.
[Note: Example of palindrome number is 424]

t) Write a program that asks for any octal number and calculates its decimal equivalent.

u) Write a program that asks for any three numbers and displays the difference
between the greatest and the smallest among them.

v) Write a program that asks for any three numbers and displays the middle one i.e.
neither the greatest nor the smallest among them.

w) Write a program that asks for any number and displays its factors.

x) Write a program that asks for any number and calculates its factorial. [Hint:
Factorial of 5 = 5×4×3×2×1 i.e. 120]

y) Write a program that asks for multi-digit integer and displays the product of its
individual digits.

Nested Loop

z) Write a program to display only Armstrong numbers between 1 and 500.

aa) Write a program to display only prime numbers between 100 and 400.

bb) Write programs to display the following numeric patterns:

i) 1 ii) 1

12 21

123 321

1234 4321

12345 54321

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 270

Vedanta Let’s Log in Computer Science | Book 9 iv) 5 4 3 2 1
5432
iii) 5 543
54 54
543 5
5432
54321 vi) 5 4 3 2 1
4321
v) 1 2 3 4 5 321
1234 21
123 1
12
1 viii) 1
22
vii) 5 5 5 5 5 333
4444 4444
333 55555
22
1

ix) 5
44
333
2222
11111

271 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

4.2.6 Library Functions

Chapter Includes:

Types of Functions
String Manipulation Functions
Mathematical Functions
Other Useful Functions and Statements

Function

Function in QBASIC is a block of statement or a readymade program which takes
some data as parameter(s), manipulates them, and returns the value in string or
numeric type.

Type of Function

There are two types of functions available in QBASIC:
Library Function
User-Defined Function

Structure of a Function

function_name (parameters)

parameters – these are the requirement for the function to perform any task. For
example, if a function adds any two numbers, these two numbers are the parameters
for this function.

Library Function

Library function is already available in QBASIC. Therefore, it is also called a Built-
in function or intrinsic function. Programmer does not need to define this function.
Here, some library functions of QBASIC are discussed below:

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 272

Vedanta Let’s Log in Computer Science | Book 9

a) String manipulation functions
i) LEN function

Purpose: to execute a string processing function that returns the number
of characters in a string or the number of bytes required by a variable
Syntax:

LEN(stringexpression)
or
LEN(variable)

stringexpression is a string constant or string expression
variable identifies the object for which you want the number of
required bytes

Example:

CLS

a$ = "PROGRAMMING"

b% = 1456

c& = 123

d! = 26.178

e# = 44678.155

PRINT LEN(a$) ‘ Displays 11

PRINT LEN(b%) ‘ Displays 2

PRINT LEN(c&)‘ Displays 4

PRINT LEN(d!) ‘ Displays 4

PRINT LEN(e#) ‘ Displays 8

END

273 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

ii) LEFT$ function

Purpose: to execute a string processing function that returns a string
consisting of the leftmost n characters of a string
Syntax:
LEFT$(stringexpression,n)

stringexpression is a string constant, string variable, or string
expression
n, a numeric expression, must have a value between 0 and 32,767.
If n is zero, the null string (a zero-length string) is returned.

Note: You might have noticed the $(dollar sign) after function name. The function
which contains $ sign after its name always returns a string value.

iii) RIGHT$ function

Purpose: to execute a string function that returns the rightmost n
characters of a string
Syntax:
RIGHT$(stringexpression,n)

iv) MID$ function

Purpose: to execute a string-processing function that returns a
substring of a string

Syntax:

MID$(stringexpression,start,length)

stringexpression identifies the string from which the substring
is to be extracted

start, a numeric expression that has an integer value between 1 and
32,767, specifies the starting character position of the substring

length can be omitted if you want all the characters to the right
of start

Example of using LEFT$, RIGHT$, and MID$ functions:

CLS

A$ = "PROGRAMMING"

PRINT LEFT$(A$, 7) ‘ Displays PROGRAM

PRINT RIGHT$(A$, 8) ‘ Displays GRAMMING

PRINT MID$(A$, 5, 3) ‘ Displays RAM

PRINT MID$(S$, 8) ‘ Displays MING

END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 274

Vedanta Let’s Log in Computer Science | Book 9

v) CHR$ function

Purpose: to execute a string processing function that returns a one-
character string whose ASCII code is the argument
Syntax:

CHR$(code)
code, a numeric expression that has a value between 0 and 255,
is one of the ASCII character codes

vi) ASC function
Purpose: to execute a string processing function that returns a numeric
value that is the ASCII code for the first character in a string expression
Syntax:
ASC (stringexpression)

Characters ASCII Value
A-Z 65-90
a-z 97-122
0-9 48-57
Space 32
Enter 13

Example: ‘ Displays d
PRINT CHR$(100) ‘ Displays 65
PRINT ASC("ABC")

vii) VAL function

Purpose: to execute a string-processing function that returns the
numeric value of a string of digits
Syntax:
VAL(stringexpression)

viii) STR$ function

Purpose: to execute a string function that returns a string
representation of the value of a numeric expression
Syntax:
STR$(numeric-expression)

275 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

Example:

CLS

x=2
y=3
a$ = "3"
b$ = "4"
PRINT x + y ‘ Displays 5
PRINT a$ + b$ ‘ Displays 34
PRINT STR$(x) + STR$(y) ‘ Displays 23
PRINT VAL(a$) + VAL(b$) ‘ Displays 7
END

ix) SPACE$ function

Purpose: to execute a string processing function that returns a string
of spaces of length n
Syntax:
SPACE$(n)

n, a numeric expression that has an integer value between 0
and 32,767, is the number of spaces you want in the string
Example:
CLS
PRINT "HAMI"; SPACE$(5); "NEPALI"
END

x) STRING$ function

Purpose: to execute a string processing function that returns a string
whose characters all have a given ASCII code or whose characters are
all the first character of a string expression

Syntax:
STRING$(m,n)
STRING$(m,stringexpression)
m, a numeric expression, is the length of the string to return
n, a numeric expression that has an integer value between 0
and 255, is the ASCII character code of the character that fills
the string
stringexpression identifies a string whose first character can be
used to fill the string

Example:
CLS
PRINT STRING$(4, 65) ' prints AAAA
PRINT STRING$(2, "Nepal") ' prints NN
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 276

Vedanta Let’s Log in Computer Science | Book 9

xi) DATE$
As a statement
Purpose: to set the current system date

Syntax:
DATE$ = stringexpression

stringexpression must have one of the following forms:
mm-dd-yy, mm-dd-yyyy, mm/dd/yy, or mm/dd/yyyy where mm
and dd are the month and day, and yy or yyyy are the year

Example:
DATE$ = "03-15-2020"
Sets the system date as 15th March 2020
As a function
Purpose: To return a string containing the current system date

Syntax:
DATE$

a ten-character string is returned, in the form mm-dd-yyyy
mm is the month (01-12),
dd is the day (01-31), and
yyyy is the year (1980-2099)

Example:
PRINT DATE$
Displays the current system date

277 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

xii) TIME$

As a statement
Purpose: To set the current time
Syntax:
TIME$=stringexpression

stringexpression must have one of the following forms:
hh, hh:mm, or hh:mm:ss where hh is the hour on a 24-hour
clock, mm is the minutes, and ss is the seconds
Example:
TIME$ = "07:15:45"
Sets the system time as 7 Hour 15 Minutes and 45 Seconds
As a function
Purpose: To return the current system time
Syntax:
TIME$
an eight-character string is returned, in the form hh:mm:ss
hh is the hour (00-23), mm is the minute (00-59), and ss is the
second (00-59)
Example:
PRINT TIME$
Displays the current system time

xiii) LTRIM$ function
Purpose: to set a function that returns a copy of a string with leading
spaces removed
Syntax:
LTRIM$(stringexpression)

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 278

Vedanta Let’s Log in Computer Science | Book 9

xiv) RTRIM$ function
Purpose: to set
a string processing function that returns a string with trailing (right-
hand) spaces removed

Syntax:
RTRIM$(stringexpression)

Example:
B$ = “ QBASIC”
C$ = “PROGRAMMING ”
B$=LTRIM$(B$)
C$=RTRIM$(C$)
PRINT LEN(B$);LEN(C$)
END

b) Mathematical Function
i) ABS function

Purpose: to set
a math function that returns the absolute value of a numeric expression

Syntax:
ABS(numeric-expression)

Example:
n = -53
PRINT ABS(n)

279 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

ii) SGN function

Purpose: to set a math function that indicates the sign of a numeric
expression

Syntax:
SGN(numeric-expression)

If numeric-expression is positive, the SGN function returns +1.
If numeric-expression is zero, the SGN function returns 0.
If numeric-expression is negative, the SGN function returns -1.
Example:
CLS
X = 15
Y=0
Z = -12.3
PRINT SGN(X) ' prints 1
PRINT SGN(Y) ' prints 0
PRINT SGN(Z) ' prints -1
END

iii) SQR function

Purpose: to execute a math function that returns the square root of
numeric expression

Syntax:
SQR(numeric-expression)

numeric-expression must have a value greater than or equal to
zero
Example:
CLS
C = 16
A = SQR(C)
PRINT "Square root of "; C; " is"; A
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 280

Vedanta Let’s Log in Computer Science | Book 9

iv) INT function

Purpose: to set a math function that returns the largest integer less
than or equal to a numeric-expression
Syntax:
INT(numeric-expression)

v) FIX function

Purpose: to set a math function that returns the truncated integer
part of a numeric expression
Syntax:
FIX(numeric-expression)

vi) CINT function
Purpose: to execute a conversion function that converts a numeric
expression to an integer by rounding the fractional part of the expression
Syntax:
CINT(numeric-expression)

Example:

INT (X) X=4 X= 4.7 X = -4.9
FIX (X) 4 4 -5
CINT (X) 4 4 -4
4 5 -5

vii) SIN function

Purpose: to execute a math function that returns the sine of an angle
given in radians
Syntax:
SIN(numeric-expression)

numeric-expression is the angle, expressed in radians.

281 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

viii) COS function
Purpose: to set
a math function that returns the cosine of an angle given in radians
Syntax:
COS(numeric-expression)

ix) TAN function
Purpose: to set
a math function that returns the tangent of an angle expressed in
radians
Syntax:
TAN(numeric-expression)

Example:
B = 60
PRINT SIN(B)
PRINT COS(B)
PRINT TAN(B)
c) Other useful functions and statements
i) SPC function

Purpose: to execute
an I/O function that skips n spaces in a PRINT statement
Syntax:
SPC(n)

n, a numeric expression that returns an integer value between
0 and 32,767, is the number of spaces you want in print line

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 282

Vedanta Let’s Log in Computer Science | Book 9

Example:
CLS
PRINT "QBASIC"; SPC(5); "PROGRAMMING"
END
Output:
The SPC function returns 5 spaces which are printed as below.
QBASIC PROGRAMMING
ii) TAB function

Purpose: to execute
a device I/O function that moves the print position when used in the
PRINT statement
Syntax:
TAB(column)

column, a numeric expression that has an integer value between
1 and (column-width of the display device )
Note:
In normal mode, the column-width of the screen is 80.
Example:
CLS
PRINT "QBASIC"; TAB(5); "PROGRAMMING"
END

Output:
The TAB() function moves the print position to the 5th column of the
screen. So, the string constant “PROGRAMMING” is printed from the
5 column, i.e., after 4 spaces in the second line as below:
QBASIC

PROGRAMMING

283 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

iii) LOCATE statement

Purpose: to execute an I/O statement that moves the cursor to the
specified position
Syntax:
LOCATE [row],[column]

row and column to move the cursor
Example:
CLS
PRINT SPC(5); "NEPAL"
PRINT TAB(5); "NEPAL"
LOCATE 5, 2
PRINT "NEPAL"
END

Output:

Column‘ 1 2 3 4 5 6 7 8 9 10 Returns 5 spaces
Row ’ by SPC function
1 NEPAL Moves the cursor

2 NEPAL to 5th Column
3
4 Prints from 5th
5 NEPA L row & 2nd column
6

Sample Program

i) To display the reverse of a string
CLS
INPUT "Type your name "; n$

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 284

Vedanta Let’s Log in Computer Science | Book 9

FOR i = LEN(n$)TO 1 STEP -1
c$ = MID$(n$, i, 1)

r$ = r$ + c$
NEXT i
PRINT "Reversed = "; r$
END

ii) To count the frequency of vowels
CLS
INPUT "Type any string "; s$
M$ = LCASE$(s$)
FOR i = 1 TO LEN(M$)
b$ = MID$(M$, i, 1)
IF b$ = "a" OR b$ = "e" OR b$ = "i" OR b$ = "o" OR b$ = "u" THEN
c=c+1
END IF
NEXT i
PRINT "Frequency of vowels = "; c
END

iii) To display the following pattern
PROGRAMMING
ROGRAMMIN
OGRAMMI
GRAMM
RAM
A

CLS
b$ = "PROGRAMMING"
n = 11
FOR i = 1 TO 6

PRINT TAB(i); MID$(b$, i, n)

285 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

n=n-2
NEXT i
END

iv) To change the letters in lowercase without using LCASE$() function
CLS
s$ = "COMPUTER"
FOR i = 1 TO 8
b$ = MID$(s$, i, 1)
c = ASC(b$)
d$ = d$ + CHR$(c + 32)
NEXT i
PRINT "In lowercase: "; d$
END

v) To calculate hex equivalent of a decimal no.
INPUT "DECIMAL NO:"; D
DO WHILE D <> 0
R = D MOD 16
IF R < 10 THEN
A$ = LTRIM$(STR$(R)) + A$
ELSE
R = R + 55
A$ = CHR$(R) + A$
END IF
D = D \ 16
LOOP
PRINT "HEXADECIMAL EQUIVALENT:"; A$

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 286

Vedanta Let’s Log in Computer Science | Book 9

Exercise

1. Answer the following questions:
a) Define function. Mention its types.
b) What are library functions? List any six built-in functions available in QBASIC.
c) Differentiate between library function and user-defined function.

2. Debug the following programs:

a) REM Reversing a string

INPUT "Type any number "; n$

FOR p = LEN(n$) TO 1

b$ = MID$(n$, 1, p) + b$

NEXT q

PRINT "Reversed = "; b$

END

b) REM Printing a string in a pattern

DIM A AS STRING

S = PROGRAMMING

FOR P = 1 TO LEN$(A)

PRINT LEFT (A, P)

NEXT S

END

c) CLS

BROWSER=”BROWSER”

BROWSERLEN$=LEN(BROWSER$)

FOR KOUNTER=1 TO BROWSERLEN

PRINT MID(BROWSER$,KOUNTER,1);

NEXT BOUNCER

END

287 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

3. Write down the output of the following programs:
a) CLS
FOR i = 5 TO 1 STEP -1
PRINT RIGHT$("NEPAL", i)
NEXT i
END
b) CLS
T$ = "CQMPAEBILS"
FOR I = 1 TO 6
READ N
PRINT MID$(T$, N, 1);
NEXT I
DATA 2,7,5,10,8,1
END
c) CLS
FOR I = 1 TO 5
READ N$, A
PRINT MID$(N$, A, 1);
NEXT I
DATA COMPUTER,4,ORACLE,1,WINDOW,6
DATA KEYBOARD,2,FORTRAN,5
END
d) CLS
a = 97
FOR i = 5 TO 1 STEP -1
c$ = CHR$(a)
d$ = CHR$(a - 32)
PRINT ASC(c$); TAB(10); c$
PRINT ASC(d$); TAB(10); d$
a=a+2

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 288

Vedanta Let’s Log in Computer Science | Book 9

NEXT i
END
e) S$ = "COMPUTER"
L = LEN(S$)
C=1
WHILE L > 0
T$ = LEFT$(S$, L)
IF L / 2 = L \ 2 THEN
PRINT C; T$
C=C+1
END IF
L=L-1
WEND

4. Write down the QBASIC code for the following problems:

a) Write a program that asks for any string and counts the frequency of character
‘e’ present in the supplied string.
Test Data :
Type any string value: elephant
Expected Output:
Frequency of e: 2

b) Write a program that asks for any string value and checks whether it is
palindrome. [A string is palindrome if it is same when spelled from reverse order
also. Eg. racecar]

c) Write a program that asks for your name and counts the frequency of consonants
present in the supplied name.

d) Write a program that asks for any number and checks whether the supplied
number is a whole number.

e) Write a program that asks for any string value and displays only the vowels from
the supplied string.

289 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

f) Write a program that asks for any numbers and checks whether it is positive,
negative, or zero.

g) Write a program that asks for any number and checks whether it is perfect
square or not.

h) Write a program that checks whether the supplied character is capital alphabet,
small alphabet, numeric character, or other symbol.

i) Write a program that asks any integer and calculates its binary equivalent.

j) Write a program that asks the date of birth of a person in MM-DD-YYYY format
and checks whether he/she was born in the current month.

k) Write a program that asks for any decimal number and calculates its hexadecimal
equivalent.

l) Write a program that asks for any hexadecimal number and calculates its
decimal equivalent.

m) Write a program that asks for multiple words and displays only their initial letters.
[For example: Central Processing Unit should give CPU]

n) Write a program that asks for your name in any case and displays it in opposite
case without using UCASE$() and LCASE$() function [Eg. NePaL should give
nEpAl].

o) Write a program that asks any for string and counts the number of characters
present in the supplied string without using LEN() function.

p) Write a program to display the letter from d to n using a build-in function and a
looping statement.

q) Write programs to display the following string patterns:

i) N ii) H E L L O

NE HE L L

NE P HE L

NE P A HE

NE P AL H

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 290

Vedanta Let’s Log in Computer Science | Book 9

iii) Q B A S I C iv) K

BASI C NK

ASI C ANK

SI C HANK

IC T HANK

C

v) M O U S E vi) Y
OUSE RY
USE
SE ORY
E T ORY
ST ORY

vii) L viii) E
L
O
G B
I A
T
C

A N
ix) R A M NG

G R A MM
O G R A MMI
R O G R A MMI
P R O G R A MMI

291 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

4.2.7 Array

Chapter Includes:

Advantages of an Array
One-Dimensional Array
Searching and Sorting in an Array
Two-Dimensional Array

You have already used variables in your programs many times. The limitation of
a variable is that, it can hold only one data item at a time. If you update or change
the value of a variable, its previous value is overwritten.

Array has a single name; but it can hold multiple values.

Array is a data structure that holds a collection of data, all of the same type and
accessed using a common name. The individual data item in an array is called an
Element.

Advantages of an Array

Array can store multiple values of same data type. So, it reduced the number
of variables.
Program is more efficient and well managed.
Sorting and searching the data can be done easily.

Array Declaration

To declare an array in QBASIC, we use DIM statement.

Declaring One Dimensional Array

Syntax:
DIM Array_Name(subscript)

A subscript is a value that is used to declare the size of array.
Example:
DIM A%(5)

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 292

Vedanta Let’s Log in Computer Science | Book 9

Here the array name is A with data-type integer and value of subscript is 5, that is
you can store maximum 5 integers in this array.

Note: One-dimensional array represents multiple data items as a list while two-dimensional
array represents multiple data items as a table consisting of rows and columns.

Storing Data in an Array

CLS
DIM A%(5)
FOR i = 1 TO 5

INPUT "Any integer "; A%(i)
NEXT i
END

Note: If you use an array in your program without including the array in a DIM statement, the
maximum value of each subscript of the array is 10.
Here, the statement
INPUT "Any integer "; A%(i)
is executed 5 times and if we supply 5 integers 67,12,78,31,10 respectively. The
integers is stored in the array in this way:

67 12 78 31 10
A(1) A(2) A(3) A(4) A(5)

Sample Program

REM to find the sum and average of 10 integers
DIM n%(10)
FOR i = 1 TO 10

INPUT "Any integer "; n%(i)
sum = sum + n%(i)
NEXT i
avg = sum / 10
PRINT "Sum = "; sum
PRINT "Average = "; avg
END

293 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

Searching for Data in an Array

Linear Search or Sequential Search

Once the data are stored in an array, we can search for a particular data by
comparing each element stored in the array one after another in a sequential order.
This type of search method is called linear search. The search process stops when
either the value is found or the end of the array is encountered.

Program of Sequential/Linear Search

REM Linear search from the data
CLS
DIM n(10)
FOR i = 1 TO 10

READ n(i)
NEXT i
INPUT "Type a number to search"; x
FOR a = 1 TO 10

IF n(a) = x THEN
check = 1
EXIT FOR

END IF
NEXT a
IF check = 1 THEN

PRINT "Data Found!!!"
ELSE

PRINT "Data Not Found!!!"
END IF
DATA 34,17,57,27,87,49,50,39,12,42
END

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 294

Vedanta Let’s Log in Computer Science | Book 9

Sorting Data in an Array

Sorting means the arrangement of data in ascending or descending order numerically
or alphabetically. Data can be sorted in various methods like Quick sort, Insertion
sort, Bubble sort, Heap sort, Shell sort, etc. But we are using only bubble sort.
Normally, we sort the data in order to search for the data in an efficient manner.

Bubble Sort

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through
the list to be sorted, comparing each pair of adjacent items, and swapping them if
they are in the wrong order. The pass through the list is repeated until no swaps
are needed, indicating that the list is sorted. The algorithm gets its name from the
way smaller elements "bubble" to the top of the list.

Program of Bubble Sort

REM Bubble sort
CLS
DIM n(10)
FOR i = 1 TO 10

READ n(i)
NEXT i
FOR x = 10 TO 1 STEP -1

FOR y = 1 TO x - 1
IF n(y) > n(y + 1) THEN SWAP n(y), n(y + 1)

NEXT y
NEXT x
PRINT "Sorted data..."
FOR x = 1 TO 10

PRINT n(x);
NEXT x
DATA 34,17,57,27,87,49,50,39,12,42
END

295 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

Two dimensional array

In two dimensional array, the data are stored in a tabular form in rows and columns.

Syntax:
DIM array_name(m,n)

m, a number that declares the number of rows
n, a number that declares the number of columns

Example:
DIM N%(3,4)
CLS
DIM N%(3, 4)
FOR x = 1 TO 3

FOR y = 1 TO 4
INPUT "Any number "; N%(x, y)

NEXT y
NEXT x
END
Here, the number of rows is 3 and number of columns is 4. So, the data is stored in
the following format:

N(1,1) N(1,2) N(1,3) N(1,4)
N(2,1) N(2,2) N(2,3) N(2,4)
N(3,1) N(3,2) N(3,3) N(3,4)

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 296

Vedanta Let’s Log in Computer Science | Book 9

POINTS TO REMEMBER

Array is a data structure that holds a collection of data, all of the same type, and
accessed using a common name.
Types of Array: One-dimensional, Two-dimensional
Array is declared using DIM statement.
Array makes sorting and searching the data easy and fast.

Exercise

1. Answer the following questions.
a) Define array. Write down its advantages.
b) What is the main difference between a variable and an array?
c) Differentiate between one-dimensional and two-dimensional array.

2. Debug the following programs:
a) REM to print the longest string
DATA Tourism, Transport, Service, Agriculture, Trade
READ L$
FOR C=1 TO 5
READ x$
IF LEN$(x$)>LEN$(L$) THEN x$=L$
NEXT C
PRINT “Longest String is; L$”
END

297 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

Vedanta Let’s Log in Computer Science | Book 9

b) REM to display the greatest number among 10
CLS
DIM a(12)
FOR x = 1 TO 10
READ j(x)
NEXT x
FOR j = 1 TO 9
IF max < a(x) THEN
a(j)= max
END IF
NEXT j
PRINT “max”
DATA 10,23,33,44,33,55,16,23,22,21

3. Write down the QBASIC code for the following problems:

a) Write a program that asks for any 10 numbers and stores into an array, and finds
their sum and average.

b) Write a program that asks for 15 numbers and stores into an array, and displays
only the odd item from them.

c) Write a program that asks for any 10 numbers and stores into an array, and
displays the smallest number among them.

d) Write a program that asks for any 10 numbers and displays them in descending
order.

e) Write a program that asks for any 5 names and displays them in alphabetical
order [Same as bubble sort as written in previous page].

f) Write a program that stores name and email address of 5 students and stores
them in a two-dimensional array, and displays all the data in a tabular form.

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 298

Vedanta Let’s Log in Computer Science | Book 9

i. CPU Abbreviation
ii. GIGO
iii. CAD : Central Processing Unit
iv. CBT : Garbage-In-Garbage-Out
v. CAE : Computer Aided Design
vi. CBL : Computer Based Training
vii. ATM : Computer Aided Education
viii. IRC : Computer Based Learning
ix. CAM : Automated Teller Machine
x. MHz : Internet Relay Chat
xi. GHz : Computer Aided Manufacturing
xii. CAE : Mega Hertz
xiii. HP : Giga Hertz
xiv. DOS : Computer-Aided Engineering
xv. WiFi : Hewlett-Packard
xvi. PDA : Disk Operating System
xvii. GSM : Wireless Fidelity
xviii. GPRS : Personal Digital Assistant
xix. DDP : Global System for Mobile
xx. AMD : General Packet Radio Service
xxi. ALU : Distributed Data Processing
xxii. MU : Advanced Micro Devices
xxiii. CU : Arithmetic-Logic Unit
xxiv. RAM : Memory Unit
xxv. ROM : Control Unit
xxvi. DRAM : Random Access Memory
xxvii. SRAM : Read-Only Memory
xxviii. PROM : Dynamic Random Access Memory
xxix. EPROM : Static Random Access Memory
xxx. EEPROM : Programmable Read-Only Memory
xxxi. PCB : Erasable Programmable Read-Only Memory
xxxii. TB : Electrically Erasable Programmable Read-Only Memory
xxxiii. RPM : Printed Circuit Board
xxxiv. WORM : Terabyte
: Revolutions Per Minute
: Write Once Read Many

299 Approved by Curriculum Development Centre, Sanothimi, Bhaktapur

xxxv. DVD-ROM Vedanta Let’s Log in Computer Science | Book 9
xxxvi. SSD
xxxvii. USB : Digital Video Disc-Read-Only Memory
xxxviii. ASCII : Solid State Drive
xxxix. LCD : Universal Serial Bus
xl. LED : American Standard Code for Information Interchange
xli. OLED : Liquid Crystal Display
xlii. CRT : Light Emitting Diode
xliii. LCD : Organic Light Emitting Diode
xliv. DLP : Cathode Ray Tube
xlv. OS : Liquid Crystal Display
xlvi. VMS : Digital Light Processing
xlvii. CUI : Operating System
xlviii. GUI : Virtual Memory System
xlix. TIF : Character User Interface
l. SVG : Graphical User Interface
li. EPS : Tagged Image Format
lii. PDF : Scalable Vector Graphic
liii. JPG : Encapsulated Post Script
liv. GIF : Portable Document Format
lv. PPI : Joint Photographic Experts Group
lvi. DPI : Graphics Interchange Format
lvii. ARPANET : Pixels Per Inch
lviii. NSFNet : Dots Per Inch
lix. WWW : Advanced Research Project Agency Network
lx. HTTP : National Science Foundation Network
lxi. HTTPS : World Wide Web
lxii. IoT : Hypertext Transfer Protocol
lxiii. URL : Hypertext Transfer Protocol Secure
lxiv. DNS : Internet of Things
lxv. CSS : Uniform Resource Locator
lxvi. RGB : Domain Name System
lxvii. SDLC : Cascading Style Sheet
lxviii. DFD : Red Green Blue
lxix. QBASIC : Software Development Life Cycle
: Data Flow Diagram
: Quick Beginners All purpose Symbolic Instruction Code

Approved by Curriculum Development Centre, Sanothimi, Bhaktapur 300


Click to View FlipBook Version