The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by myravu, 2020-05-22 09:15:45

New Gateway to Computer Science 9

New Gateway to Computer Science 9

Keywords: School Text Book

Program 5: Write a Qbasic program to enter any number and check whether
it is negative or positive number.
CLS
Input "Enter the number"; N
If N>0 Then
Print "The number is positive"
Else
Print "The number is negative"
EndIf
End

Program 6: Write a Qbasic program to enter any number and find out whether
it is even or odd using select case statement.
CLS
Input "Enter any number" ;N
Rem=N mod 2
Select case Rem
Case = 0
Print "The number is Even number"
Case Else
Print "The number is odd number"
End Select
End

Program 7: Write a Qbasic program to check the numbers between 1 and 5.
CLS
Input "Enter the numbers between 1-5";N
Select case N
Case 1
Print "This is number 1";
Case 2
Print "This is number 2";
Case 3
Print "This is number 3"
Case 4

250 New Gateway to Computer Science Book -9

Print "This is number 4"
Case 5
Print "This is number 5"
Case else
Print "Invalid number range ";
End select
End

Program 8: Write a QBASIC program to enter any alphabet and test whether

the alphabet is ‘a’ or not using the select case statement.
CLS
Input "Enter the alphabet";A$
A$=UCase$ (A$)
Select Case A$
Case 'A'
Print "It’s alphabet A"
Case Else
Print "It’s not alphabet A"
End Select

End

Program 9: Write a QBASIC program to enter any alphabet and find out

whether the alphabet is vowel or not.

CLS

Input "Enter Letters"; A$

A$ = UCase $ (A$)

Select case A$

Case "A", "E", "I", "O", "U"

Print "It is vowel"

Case Else

Print "It is not a vowel"

End Select

End

New Gateway to Computer Science Book -9 251

Program 10: Write a QBASIC program to generate the following series using
For….Next…..Loop.

1,3,5,7,9, 11, 13, 15………………...99

CLS

For I = 1 to 99 Step 2

Print I
Next I

End

Program 11: Write a QBASIC program to generate the following series using
For….Next…..Loop.

5,10,15,20,25…100

CLS

For I = 5 to 100 Step 5

Print I

Next I

End

Program 12: Write a QBASIC program to print numbers from 1 to 100 using
WHILE…WEND statement.

CLS

I=1

While I<=100

Print I ;

I=I+1

WEND

END

Program 13: Write a QBASIC program to print series as below using

WHILE…WEND statement.

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

CLS

I=1

While I < =10

Print I^2;

I=I+1

WEND

END

252 New Gateway to Computer Science Book -9

Program 14: Write a QBASIC program to read any 10 different numbers and

find the greatest number among them.

CLS

READ G

FOR I = 2 to 10

READ N

IF N>G THEN G = N

DATA 19, 2, 56, 11, 76, 10, 22, 12, 55, 1

NEXT I

PRINT "The greatest Number is:"; G

END

Program 15: Write a QBASIC program to find out the factorial of a given

number (n).

CLS

REM' PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER

INPUT "ENTER A NUMBER:"; N

F=1

FOR I = 1 TO N

F=F*I

NEXT I

PRINT "THE FACTORIAL OF THE NUMBER IS", F

END

Program 16: Write a QBASIC program to find check whether the entered

number is palindromic or not.

CLS

INPUT "ENTER ANY NUMBER"; N

A=N

S=0

WHILE N <> 0 A palindromic number is a number that is the same when
written forwards or backwards. The few palindromic
R = N MOD 10

S = S * 10 + R numbers are 0 to 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101,

N = N \ 10 111, 121 and so on

WEND

New Gateway to Computer Science Book -9 253

IF A = S THEN

PRINT A; "This is a palindromic number."

ELSE

PRINT A; "This is not a palindromic number."

END IF

END

Program 17: Write a QBASIC program to check whether the entered number

is prime or composite.

CLS A prime number is a whole number that has
INPUT "Enter any number"; N only two factors which are itself and one. A
A=0 composite number has factors in addition to
FOR I = 1 TO N one and itself.

IF N MOD I = 0 THEN A = A + 1 The numbers 0 and 1 are neither prime nor
NEXT I composite.
IF A = 2 THEN
All the even numbers are divisible by two
PRINT "It is prime number"; and so all even numbers greater than two are
ELSE composite numbers.All the numbers that end in
five are divisible by five. Therefore all numbers
PRINT "It is composite number"; that end with five and are greater than five are
END IF composite numbers.

END The prime numbers between 2 and 100 are 2, 3,
5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53,59, 61, 67, 71, 73, 79, 83, 89 and 97.

Program 18: Write a QBASIC program to display the prime numbers from

2 to 100.

Method 1
CLS
FOR I = 1 TO 100
C=0
FOR J= 1 TO i
IF I MOD J = 0 THEN C = C + 1
NEXT J
IF C = 2 THEN PRINT I;
NEXT I

END

254 New Gateway to Computer Science Book -9

Method 2
CLS
FOR I = 2 TO 100
FOR J = 2 TO I - 1
IF I MD J = 0 THEN GOTO DD

NEXT J
PRINT J;
DD:
NEXT i
END

Program 19: Write a QBASIC program to display the Fibonacci sequence up

to 10th terms.

CLS

A=1 The Fibonacci sequence is the series of numbers as below.

B=1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...................

PRINT A, B, Where, the next number is found by adding up the two

FOR I = 1 TO 10 numbers before it.
• The 2 is found by adding the two numbers before it (1+1).
C=A+B
• The 3 is found by adding the two numbers before it (1+2).
PRINT C,
• The 5 is found by adding the two numbers before it (2+3)
A=B and so on.
B=C

NEXT I

END

Program 20: Write a QBASIC program to check whether the entered number

is Armstrong or not. The number which is formed by the sum of

Method 1:Using DO………LOOP cubes of its own digits is Armstrong number.
CLS 153, 370, 371, 407 are the Armstrong
INPUT "Enter a number"; N numbers.
B=N
Let’s take an example of 153.

DO WHILE N<> 0 153 = 13 + 53 + 33

R = N MOD 10 = 1 + 125 + 27

A = A +R ^ 3 = 153

New Gateway to Computer Science Book -9 255

N = N \ 10
LOOP
IF A = B THEN
PRINT "It is an Armstrong number";
ELSE

PRINT "It is not an Armstrong number";
END IF

END

Method 2: Using WHILE………WEND
CLS
INPUT "ENTER ANY NUMBER"; N
A=N
S=0
WHILE N <> 0
R = N MOD 10
S=S+R^3
N = N \ 10
WEND
IF A = S THEN
PRINT A; "It is an ARMSTRONG number."
ELSE
PRINT A; "It is not an ARMSTRONG number."
END IF

END
Program 21: Write a QBASIC program to display the output as below.

1 CLS
222 FOR I = 1 TO 6
33333 LOCATE I, 50 - I
4444444 FOR J = 1 TO I * 2 - 1
555555555 PRINT USING "#"; I;
666666666666 NEXT
PRINT
NEXT
END

256 New Gateway to Computer Science Book -9

Program 22: Write a QBASIC program to display the reverse of a given

number.

CLS

INPUT "Enter the number to reverse"; N

WHILE N <> 0

R = N MOD 10

S = S * 10 + R

N = N \ 10

WEND

PRINT "The reverse of the given number is"; S

END

Assignment

1. Fill in the blanks with suitable words.

a. ……….. allow you to change the computer's control from automatically reading the
ext line of code to reading a different one.

b. …………… statement allows the selective execution of statements based on a
particular condition.

c. ………….. statement is also called two way decision making statements.

d. The IF….THEN…..ELSE statement is another form of ………….. statement in
which the conditions are evaluated from top to button.

e. The ………….. statement in QBASIC is used to transfer the program control
from one statement to another.

f. The ……………. loop is used when we are required to perform repetitive tasks.

g. A ………….. loop is used when you want to repeat a set of statements as long as
the condition is false.

2. State whether the given statements are true or false.

a. A program consists of a number of statements which are usually executed in
sequence.

b. Branching statement is not a decision making statement.

New Gateway to Computer Science Book -9 257

c. IF... THEN statement is a decision making conditional branching statement
which executes a block of statements only if the given expression or condition is
true.
d. Conditional branching is when the programmer forces the execution of a program
to jump to another part of the program.
e. Looping statement is the statements that executes one or more statement
repeatedly in a several number of times.
f. The SWAP statement is used to terminate the loop based on certain criteria before
the expected number of iterations.
3. Write down the function and syntax for the following statements.
a. IF…….THEN statement
b. IF…….THEN……ELSE statement
c. SELEST CASE statement
d. GOTO statement
e. FOR….NEXT statement
f. WHILE…….WEND statement
g. DO….WHILE LOOP statement
h. DO………UNITL LOOP statement
i. EXIT statement

4. Debug the following programs.

a. CLS b. CLS
INPUT "Enter your marks in computer"; C$ C=1
C >= 40 THEN DO NEXT C = 21
PRINT "You are passed." PRINT M
ELSE C=C+2
PRINT "You are failed ." LOOP WHILE
IF END
END

258 New Gateway to Computer Science Book -9

c. CLS d. CLS
C=1
INPUT "Enter your choice (1 – 3)"; N WHILE B <= 10
SELECT CASE Z PRINT
CASE 1
PRINT "Baishak" C=b+1
CASE 2 END
PRINT "Jestha" WEND
CASE 5
PRINT "Ashad"
ELSE CASE
PRINT "Invalid Value"
SELECT END

END

5. Write down the output of the following programs.

a. CLS b. CLS
For I = 1 to 99 Step 2 I=1
Print I While I < =10
Next I Print I^2;
End I=I+1
WEND
END

c. CLS f. CLS
FOR I = 1 TO 6
LOCATE I, 50 - I A=1
FOR J = 1 TO I * 2 - 1 FOR I = 1 TO 5
PRINT USING "#"; I; FOR J = 1 TO I
NEXT PRINT A;
PRINT A = A+I
NEXT NEXT J
END PRINT
NEXT I
END

New Gateway to Computer Science Book -9 259

e. CLS d. CLS
A = 50 A=1
B = 70 B=2
FOR I = A TO B STEP 5 FOR I = 1 TO 15
FOR J = B TO 1 STEP -5 PRINT A; "/"; B
PRINT J; A = A+I
NEXT J B = B+I
PRINT NEXT I
END
NEXT I

END

6. Write a QBASIC program for the followings.

a. To enter a number and check whether it is odd or even using select case statement.

b. To find the factorial of a given number.

c. To check whether the entered number is prime or composite.

d. To check whether the given number is divisible by 5 or not.

e. To check whether the given number is palindromic or not.

f. To display the Fibonacci sequence up to 20th term.

g. To check whether the given number is Armstrong or not.

h. To display the reverse of a given number.

i. To print the sum of square of odd numbers upto 200.

j. To input any number and count the total even numbers present in it.

7. Write the program to display given series.

a. 1, 4, 9, 16, 25 b. 5, 10, 15, 20, 25 c. 100, 90, 80, 60, 50

d. 1, 3, 5, 7, 9 , 11, 13, 15 e. 2, 2, 4, 6, 10, 16 f. 10, 20, 30, 40, 50

260 New Gateway to Computer Science Book -9

15Lesson Library Functions in QBASIC

Inside the Lesson

 Introduction to library function
 User defined function
 Understanding built in function
 Numeric function
 String function
 Practice session

A function is a built-in formula or a readymade program which helps us to perform a
certain task such as mathematical, statistical, financial, logical calculations, etc. Functions
are readymade programs which take some data, manipulate them and return the value,
which may be string or numeric.There are two types of functions they are user defined
function and built-in function. Let’s discuss them in detail.

User Defined Function

A function which is written and stored by the programmer to perform a specific
task is known as a user defined functions. It is created by the programmer to perform the
operations as required. It can be numeric or string function. To define a function DEF
FN is used and it must be executed before the function. FUNCTION……………END
FUNCTION statement can be used to define the function in Qbasic. The following is the
syntax of a user defined function.

Syntax: DEF FNname(parameter list) = expression

Here, DEF FN defines a single-line function. The name you must use to call your function
is FNname. If you use this, you will be able to calculate everything using one line.

New Gateway to Computer Science Book -9 261

Practical Assignment

CLS
A = 5: B = 10
DEF FNC (x, y) = x ^ 10 / y ^ 5
TOTAL = FNC(A, B)
PRINT TOTAL
END

Built in Function

Buit in function is a pre-defined program which is provided by QBASIC to perform some
task easily. It gives many more built-in functions for manipulating strings and numbers.
Built-in functions are also called Library functions. These functions are required to be
called by the programmer to use them in a program. Some common Built-in functions are
LEN, LEFT$, RIGHT$, MID$, UCASE$, LCASE$, CHR$, etc. There are two types of
built in function.They are numeric functions and string functions. Let’s discuss them in
detail.

Numeric Functions:

Numeric function provides a faster way to evaluate many mathematical functions. Some
of the numerical functions are listed below.

ABS Function : This function is used to obtain an absolute corresponding positive value
of a numerical expression. The syntax of ABS function is given below.

ABS (x), where x is a numeric expression.

Practical Assignment

CLS
N = ABS(5 * (-3))
PRINT "The absolute value is:"; N
END

CINT Function: It is a mathematical function used to convert numeric expression to
an integer by rounding the fractional portion or it returns the integer to the given number.
The syntax of CINT function is given below.

CINT (x), where x is a numeric expression. If it is not in range ( -32768) to (+32767), an
overflow error message will appear.

262 New Gateway to Computer Science Book -9

Practical Assignment

CLS
X = CINT(105.345)
PRINT "The value is"; X
END

COS Function: It is a mathematical function which is used to obtain the cosine of x. It
can be any number or expression. To convert degrees to radians use the formula: radian =
degree*(Pi/180). Where, Pi=3.14. The syntax of COS function is given below.

COS(x) where x is the angle whose cosine is to be calculated.

Practical Assignment

CLS
INPUT "Enter an angle in degree"; A
R = A * 3.14 / 180
P = COS(R)
PRINT "COSINE OF"; A; "Degree"; "is"; P
END

SIN Function: It is also a mathematical function which is used to get the sine of x. To
convert degrees to radians use the formula: radian = degree*(Pi/180). Where, Pi=3.14.
The syntax of SIN function is given below.

SIN(x), where x is an angle expressed in radians.

Practical Assignment

CLS
INPUT "Enter an angle in degree"; A
R = A * 3.14 / 180
P = SIN(R)
PRINT "Sineof"; A; "Degree"; "is"; P
END

TAN Function: It is a mathematical function that returns the tangent of an angle expressed
in radians. To convert degrees to radians use the formula: radian = degree*(Pi/180).
Where, Pi=3.14. The syntax of TAN function is given below.

TAN(x), where x is an angle expressed in radians.

New Gateway to Computer Science Book -9 263

Practical Assignment

CLS
INPUT "Enter an angle in degree"; A
R = A * 3.14 / 180
P = TAN(R)
PRINT "The tangentof"; A; "Degree"; "is"; P
END

Let’s Review

 A function is a built-in formula or a readymade program which helps us to perform
a certain task such as mathematical, statistical, financial, logical calculations, etc.
 A function which is written and stored by the programmer to perform a specific
task is known as a user defined function.
 Built in function is a pre-defined program which is provided by QBASIC to
perform some task easily.
 Numeric function provides a faster way to evaluate many mathematical functions.
 ABS function is used to obtain the absolute corresponding positive value of
a numerical expression.
 CINT is a mathematical function used to convert numeric expression to an integer
by rounding the fractional portion.
 COS is a mathematical function which is used to obtain the cosine of x.
 TAN is a mathematical function that returns the tangent of an angle expressed in
radians.

INT Function: This function is used to obtain the largest integer that is less than or equal
to x. INT rounds negative numbers to the next more negative integer. The syntax of INT
function is given below.

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

Practical Assignment

CLS
INPUT "Enter a number"; N
Result = INT(N)
PRINT "The integer value is"; Result
END

264 New Gateway to Computer Science Book -9

SGN function: It is used to obtain the mathematical sign of x. The SGN function has the
following return values.

If the number is, SGN returns
Greater than zero 1
Equal to zero 0
Less than zero -1

The syntax of SGN function is given below.

SGN(x), where x is any numeric expression.

Practical Assignment

CLS
INPUT "Enter a number"; N
Result = SGN(N)
PRINT "The SGN value is"; Result
END

SQR Function: It is used to calculate and return the square root of non-negative number.
If the number is negative, an illegal function will appear. The syntax of SQR function is
given below.

SQR(x), where x is any numeric expression that is greater than or equal to 0.

Practical Assignment

CLS
INPUT "Enter a number"; N
Result = SQR(N)
PRINT "The square root value is"; Result
END

String Functions

QBASIC program has various built in string functions that are mainly used to operate and
manipulate string expressions. Some of the string functions are listed below.

ASC Functions: It is a string processing function which is used to return a numeric
value that is the ASCII code for the first character of the string. The ASCII values of some

commonly used characters are given below.

New Gateway to Computer Science Book -9 265

Characters ASCII Value (Code)
A-Z 65-90
a-z 97-122
0-9 48-57

The syntax of ASC function is given below.

ASC (String expression)

Practical Assignment

CLS
A = ASC("SHUBHARAMBHA")
B = ASC("gatewaycomputer")
C = ASC("4302109")
PRINT A
PRINT B
PRINT C
END

CHR$ Function : This function returns a character that corresponds to specific ASCII
values in between -128 and 127 as arguments. The syntax of CHR$ function is given
below.

CHR$ (ASCII code)

Practical Assignment

CLS
PRINT "The ASCII code of 80 is::"; CHR$(80)
PRINT "The ASCII code of 110 is::"; CHR$(110)
PRINT " The ASCII code of 50 is::"; CHR$(50)
END

LEN Function: It is a string processing function which is used to count and return to the
total number of characters in a string. The syntax of LEN function is given below.

LEN (string -expression)

266 New Gateway to Computer Science Book -9

Practical Assignment

CLS
INPUT "Enter first word"; A$
INPUT "Enter second word"; B$
INPUT "Enter Third Word"; C$
PRINT "The total number of characters in fist word are::"; LEN(A$)
PRINT "The total number of characters in second word are::"; LEN(B$)
PRINT "The total number of characters in third word are::"; LEN(C$)
END

INSTR$ Function: It is a string processing function which is used to search for one
string inside another and return to the position of first character.The syntax of INSTR$
function is given below.

INSTR([n,]x$,y$) where n is an integer expression and x$ and y$ can be any string
variable, expression or constants.

Practical Assignment

CLS
A$ = "SCIENCE"
B$ = "C"
PRINT "In the word SCIENCE, The first letter C occurs at the position:"; INSTR(A$, B$)
PRINT "In the word SCIENCE, The second letter C occurs at the position:"; INSTR(3,
A$, B$)
END

VAL Function: It is a string processing function which converts a string expression
consisting of digits into value. The extracted value is numeric in nature and can be used
for mathematical calculation. If the first character of a string is not numeric, then VAL
will return 0(zero). The syntax of VAL function is given below.

VAL(N$), Where N$ is any string expression.

Practical Assignment

CLS
A$ = "20 Computer Books"
C = VAL(A$)
B$ = "40 Maths Books"

New Gateway to Computer Science Book -9 267

CO = VAL(B$)
Total = C + CO
PRINT "The total books are"; Total
END

LEFT$ Function: This is a string processing function which is used to extract
and return to the numbers of characters from the left of a string. The syntax of LEFT$
function is given below.

LEFT$ (String expression, n) where, string expression is a string constant, variable or
expression and n is an integer expression in the range.

Practical Assignment

CLS
INPUT "Enter a Word"; C$
FOR I = 1 TO LEN(C$)
PRINT LEFT$(C$, I)
NEXT I
END

RIGHT$ Function:This string processing function is used to extract and return to
the numbers of characters from the right of a string. The syntax of RIGHT$ function is
given below.

RIGHT$ (String expression, n) where, string expression is a string constant, variable or
expression and n is an integer expression in the range.

Practical Assignment

CLS
INPUT "Enter a Word"; C$
FOR I = 1 TO LEN(C$)
PRINT RIGHT$(C$, I)
NEXT I
END

MID$ Function:This function is used to replace the part of the string variable with
another. The syntax of MID$ function is given below.

MID$(string expression,start,length) where, string expression is the string from which
the sub starting has to be extracted. Start is the starting position to extract from the string
expression and length is the number of characters to extract.

268 New Gateway to Computer Science Book -9

Practical Assignment

CLS
INPUT "Enter a Word"; c$
PRINT MID$(c$, 2, 6)
PRINT MID$(c$, 3, 4)

END

Let’s Review

 INT function is used to obtain the largest integer that is less than or equal to x.
 SQR function is used to calculate and return to the square root of non-negative
number.
 ASC is a string processing function which is used to return to a numeric value that
is the ASCII code for the first character of the string.
 LEN is a string processing function which is used to count and return to the total
number of characters in a string.
 VAL is a string processing function which converts a string expression consisting
of digits into value.
 LEFT$ is a string processing function which is used to extract and return to the
numbers of characters from the left of a string.
 RIGHT$ is a string processing function which is used to extract and return to the
numbers of characters from the right of a string.
 MID$ function is used to replace the part of the string variable with another.

SPACE$ Function: This function is used to generate a string of spaces of a specified
length. The syntax of SPACE$ function is given below.

SPACE$(n) where, n is an integer expression in the range of 0-32,767.

Practical Assignment

CLS
FOR I = 1 TO 10
N$ = SPACE$(I)
PRINT N$, I;
NEXT I
END

New Gateway to Computer Science Book -9 269

STR$ Function: It is a string processing function that is used to convert a numeric
expression to string and return to the same. The syntax of STR$ function is given below.

STR$(n) where, n is any numeric expression.

Practical Assignment

CLS
INPUT "Enter a decimal number"; N
WHILE N <> 0
R = N MOD 2
S$ = STR$(R) + S$
N=N\2
WEND
PRINT "THE BINARY EQUIVALENT VALUE IS::"; S$
END
STRING$ Function:This function is used to return to a string of a specified length made
up of a repeating character. The syntax of STRING$ function is given below.
STRING$(x,y | z$)Where, x and y are the numeric expression and Z$ is any string
expression.

Practical Assignment

CLS
FOR I = 1 TO 6
PRINT STRING$(I, "*")
NEXT I
END
LCASE$ Function: This is a string processing function which converts the string into
lower case. The syntax of LCASE$ function is given below.
LCASE$(String expression)

Practical Assignment

CLS
INPUT "Enter a word or sentence in UPPER case"; C$
PRINT LCASE$(C$)
END
UCASE$ Function: This is a string processing function which converts the string into
upper case. The syntax of UCASE$ function is given below.
UCASE$(String expression)

270 New Gateway to Computer Science Book -9

Practical Assignment

CLS
INPUT "Enter a word or sentence in LOWER case"; C$
PRINT UCASE$(C$)
END
DATE$ Function: This string function is used to display the system date. The syntax of
DATE$ function is given below.

DATE$
Practical Assignment

CLS
PRINT DATE$
END
TIME$ Function: This string function is used to display the system time. The syntax of
TIME$ function is given below.

TIME$
Practical Assignment

CLS
PRINT TIME$
END
LTRIM$ Function: It is a string processing function which removes the leading blank
space from the string expression. The syntax of LTRIM$ function is given below.

LTRIM$(String expression)
Practical Assignment

CLS
A$= " DINESH ADHIKARI "
PRINT LTRIM$(A$)
END
RTRIM$ Function: It is a string processing function which removes the trailing blank
space of the string expression. The syntax of RTRIM$ function is given below.
RTRIM$(String expression)

New Gateway to Computer Science Book -9 271

Practical Assignment

CLS
A$= " DINESH ADHIKARI "
PRINT RTRIM$(A$)
END

INPUT$ Function: It is a string processing function which waits until the
specified numbers of characters are pressed on keyboard. Users do not have to press
‘ENTER’ key after the last character is typed or pressed.The syntax of INPUT$ function
is given below.

INPUT$ (n, [# ] file number]) Where, n is the number of characters to be read. Filenum
is the number under which the file was opened.

Practical Assignment

CLS
INPUT "Enter your name in 5 digits"; N$
N$=INPUT$(5)
PRINT N$

END

Let’s Review
 SPACE$ function is used to generate a string of spaces of a specified length.
 STR$ is a string processing function that is used to convert a numeric expression
to string and return to the same.
 STRING$ function is used to return to a string of a specified length made up of a
repeating character.
 LCASE$ is a string processing function which converts the string into lower case.
 UCASE$ is a string processing function which converts the string into upper case.
 DATE$ is a string function used to display the system date.
 TIME$ string function is used to display the system time.
 LTRIM$ is a string processing function which removes the leading blank space
from the string expression.
 RTRIM$ is a string processing function which removes the trailing blank space
of the string expression.
 INPUT$ is a string processing function which waits until the specified numbers of
characters are pressed on keyboard.

272 New Gateway to Computer Science Book -9

Practical Section

Program: 1: Write a Qbasic program to check whether the entered letter is
uppercase or lowercase.

CLS

INPUT "Enter a letter";A$

U$=UCASE$(A$)

IF U$=A$ THEN

PRINT "It is capital letter"

ELSE

PRINT "It is small letter"

ENDIF

END

Program: 2: Write a Qbasic program to check a given string is palindrome or
not .

CLS

INPUT "ENTER A STRING"; S$

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

M$ = MID$(S$, I, 1)

REV$ = REV$ + M$

NEXT I

IF S$ = REV$ THEN

PRINT "THE GIVEN STRING IS PALINDROME"

ELSE

PRINT "IT IS NOT PALINDROME"

END IF

Program: 3: Write a Qbasic program to convert decimal to octal.

CLS

INPUT "ENTER A DECIMAL NUMBER"; N

WHILE N <> 0

A = N MOD 8

B$ = STR$(A)

N = FIX(N / 8)

C$ = B$ + C$

WEND

PRINT "THE OCTAL EQUIVALENT IS"; C$

END

New Gateway to Computer Science Book -9 273

Program: 4: Write a Qbasic program to convert Hexadecimal to Decimal.
CLS
INPUT "ENTER HEXADECIMAL VALUE";B$
FOR I=LEN(B$) TO 1 STEP -1
A$=MID$(B$,I,1)
C=VAL(A$)
IF A$="A" THEN C=10
IF A$="B" THEN C=11
IF A$="C" THEN C=12
IF A$="D" THEN C=13
IF A$="E" THEN C=14
IF A$="F" THEN C=15
H=H+C*16^P
P=P+1
NEXT I
PRINT "DECIMAL VALUE IS";H
END
Program: 5: Write a Qbasic program to reverse a given string.
CLS
INPUT "ENTER A STRING"; S$
FOR I = LEN(S$) TO 1 STEP -1
M$ = MID$(S$, I, 1)
REV$ = REV$ + M$
NEXT I
PRINT REV$

END

Program: 6: Write a Qbasic program to display the given pattern.

N CLS
NE A$="NEPAL"
NEP FOR I = 1 TO LEN (A$)
NEPA PRINT LEFT$(A$,I)
NEPAL NEXT I
END

274 New Gateway to Computer Science Book -9

Program: 7: Write a Qbasic program to display the given pattern.

NEPAL CLS
NEPA A$ = "NEPAL"
NEP FOR i = LEN(A$) TO 1 STEP -1
NE PRINT RIGHT$(A$, i)
N NEXT i
END

Program: 8: Write a Qbasic program to display the given pattern.

L CLS
AL A$ = "NEPAL"
PAL FOR I = 1 TO LEN(A$)
PRINT RIGHT$(A$, I)

EPAL NEXT I
NEPAL END

Program: 9: Write a Qbasic program to display the given pattern.

CLS NE
a$ = "NEPAL" EP
x = LEN(a$)

FOR i = 1 TO x – 1 PA
PRINT TAB(i); MID$(a$, i, 2) AL

NEXT i

END

Program: 10: Write a Qbasic program to display the given pattern.

CL S A
s$ = "PROGRAMMING"

t = 6 RAM
FO R i = 1 TO LE N(s$) S TEP 2 GRAMM
PR INT TA B(t); M ID$(s$ , t, i) OGRAMMI

t=t–1
NE XT i ROGRAMMIN
EN D PROGRAMMING

New Gateway to Computer Science Book -9 275

Program: 11: Write a Qbasic program to display the given pattern (flag

pattern of Nepal).
*
** CLS
A$ = “*********”
A=1

*** B = 1

**** FOR I = 1 TO 5
***** COLOR 4
PRINT MID$(A$, A, B)

* B=B+1

** NEXT I
A=1

*** B = 1

**** FOR J = 1 TO 5

***** COLOR 6
* PRINT MID$(A$, A, B)
B=B+1

* NEXT J

* FOR K = 1 TO 5
COLOR 5

* PRINT MID$(A$, K, 1)

* NEXT K

END

Program: 12: Write a Qbasic program to calculate the square root of a given

number.

REM Program to calculate the square root of a given number

CLS

INPUT "Enter any number"; n

s = SQR(n)

PRINT "Square root of "; n; " is "; s

END

276 New Gateway to Computer Science Book -9

Assignment

1. Fill in the blanks with suitable words.

a. A function is a built-in formula or a ………… which helps us to perform a certain
task such as mathematical, statistical, financial, logical calculations, etc.

b. ……………….. provides a faster way to evaluate many mathematical functions.

c. …………… is a mathematical function used to convert numeric expression to an
integer by rounding the fractional portion or it returns the integer to the given
number.

d. ………….. is a mathematical function that return to the tangent of an angle
expressed in radians.

e. …………….. function is used to calculate and return to the square root of
non-negative number.

f. …………… is a string processing function which is used to count and return to
the total number of characters in a string.

g. ……………… is a string processing function which is used to extract and return
to the numbers of characters from the left of a string.

h. ………………….. function is used to replace the part of the string variable with
another.

i. ………………. is a string processing function which converts the string into
upper case.

j. …………….. string function is used to display the system time.

2. State whether the given statements are true or false.

a. Built in function is a pre-defined program which is provided by QBASIC to
perform some tasks easily.

b. CNT function is used to obtain an absolute corresponding positive value of a
numerical expression.

c. COS is a mathematical function which is used to obtain the cosine of x.

d. SQRT function is used to obtain the largest integer that is less than or equal to x.

e. ASC is a string processing function which is used to return to a numeric value that
is the ASCII code for the first character of the string.

f. VAL is a string processing function which converts a string expression consisting
of digits into value.

g. RIGHT$ is a string processing function which is used to extract and return to
the numbers of characters from the left of a string.

h. TIME$ function is used to generate a string of spaces of a specified length.

New Gateway to Computer Science Book -9 277

i. STRING$ function is used to return to a string of a specified length made up of a
repeating character.

j. INPUT$ is a numeric function which waits until the specified numbers of
characters are pressed on keyboard.

3. Write down the function and syntax for the following statements.

a. ABS function b. TAN function c. INT function

d. SQR function e. SGN function f. CHR$ function

g. LEN function h. INSRT$ function i.VAL function

j. LEFT$ function k. MID$ function l. SPACE$ function

m. STRING$ function n. UCASE$ function o. LTRIM$ function

4. Debug the following programs.

a. CLS b. CLS

A = "20 Computer Books" INPUT "Enter a decimal number"; N

C = VAL(A$) WHILE X <> 0

B$ = "40 Maths Books" R = N MOD 2

C = VAL(B) S = STR$(R) + S$

D=N\2

Total = C + CO END

PRINT "The total books are"; T PRINT "BINARY EQUIVALENT IS::"; S$

END END

c. CLS d. CLS

INPUT "Enter a letter";A$ INPUT "ENTER A DECIMAL NUMBER"; N

U=UCASE(A$) WHILE N <> 0

IF U$=A THEN A = N MOD 8

PRINT "It is capital letter" B$ = STR$(A)

ELSE IF N = FI(C / 8)

PRINT "It is small letter" WEND

IFEND C$ = A$ + C
END PRINT "THE OCTAL EQUIVALENT IS"; C$
END

278 New Gateway to Computer Science Book -9

5. Write down the output of the following program.

a. CLS

A$="NEPAL" b. CLS
FOR I = 1 TO LEN (A$) A$ = “*********”
PRINT LEFT$(A$,I) A=1
NEXT I B=1
END FOR I = 1 TO 5
c. CLS COLOR 4
a$ = "NEPAL" PRINT MID$(A$, A, B)
x = LEN(a$) B=B+1
FOR i = 1 TO x – 1 NEXT I

PRINT TAB(i); MID$(a$, i, 2) A = 1

NEXT i B=1
END FOR J = 1 TO 5
d. CLS COLOR 6
A$ = "NEPAL" PRINT MID$(A$, A, B)
FOR I = 1 TO LEN(A$) B=B+1
PRINT RIGHT$(A$, I) NEXT J
NEXT I FOR K = 1 TO 5
END COLOR 5
PRINT MID$(A$, K, 1)

NEXT K

END

6. Write a QBASIC program to display the following outputs.

G

GA NEPAL COMPUTER G

GAT NEPA OMPUTER OGR

GATE NEP MPUTER POGRA

GATEW NE PUTER PROGRAM

GATEWA N UTER

GATEWAY TER

ER

R

New Gateway to Computer Science Book -9 279

7. Write a QBASIC program for the followings.
a. Write a program to input a number and check whether it is positive, negative or
zero
b. Write a program to check whether the entered letter in uppercase or lowercase.
c. Write a program to check whether the given string is palindrome or not.
d. Write a program to convert the decimal number into octal.
e. Write a program to reverse a given string.
f. Write a program to calculate the square root of a given number.
g. Write a program to count the number of vowels in an input string.
h. Write a program to input a string and count the number of words in it.
i. Write a program to input three strings and find the longest among them.

280 New Gateway to Computer Science Book -9

16Lesson Arrays in QBASIC

Inside the Lesson

 Introduction to array
 Advantages of using array
 Declaring and using an array
 Arrays variables
 Arrays elements
 Types of array
 Rules for declaring one dimensional array
 Sorting data in array
 Searching data in array
 Working with single and double dimensional array

An array is a collection of variables of the same data type that are referred to by common
name. When there are large lists of variables and data, it is easier to contain the data in
an array than have large amounts of separate variables to hold the data. Array is a group
of related data items having a common variable name. An array is primarily used when
groups of data items must be stored and manipulated efficiently. An array is given a name
and also a specified number of data items that it will contain; each individual item is
referred to as an element. Each element is given a unique storage location in the array and
a subscript or index value is used to identify the position of a particular element.

An array declared should indicate the three main things.
a. Name of the array.
b. Type of value to be stored in an each element.

c. Number of elements in an array.

Advantages of Using an Array

 It can reduce the number of variable name in a program because single array can
store large number of data.
 It can be used to store the similar type of data rather than using many simple variables.
 It can be used to prepare more efficient programs.

New Gateway to Computer Science Book -9 281

 It is very easy to compare and manipulate data with the help of array variable rather
than simple variables.
 It is very easy to understand and use.

Sample program to store and display data using an array.
CLS
DIM N(10)
FOR i = 1 TO 10
INPUT N(i)
Sum = Sum + N(i)
NEXT i
PRINT "The total sum is the given numbers::"; Sum
END

In the above program, N(i) is an array variable, which stores ten different values using
For…Next loop. The variable sum stores the sum of all input numbers.

Declaring and Using an Array

Arrays need to be declared before using in a program and to declare an array; we must
use a DIM statement. DIM statement plays the role of reserving internal memory space
for an array of any desired size. We have to remember the name of array, number of
elements required and data type required before declaring an array.

Syntax: DIM [SHARED] arrayname (subscript) [AS TYPE] [arrayname (subscript) [AS
TYPE]]

Here, SHARED is a keyword that is used to share the value of the variable with the
procedures. Arrayname is the name of the array variable. Subscript specifies the size of
an array and AS TYPE defines the type of array variable.

Examples

DIM age (20)

DIM name$(20)

In this example age and name$ are the name of array variables and 20 is the size of
variable which provides maximum 21 elements (o to 20) or subscripted variables for an
array.

282 New Gateway to Computer Science Book -9

Array Variables

A variable of array type holds a reference to an object. Declaring a variable of array type
does not create any array object or allocate any space for array components. It creates
only the variable itself, which can contain a reference to an array.

Subscripted Variables or elements of an array Data stored in each element
N(0) 67
N(1) 7
N(2) 44
N(3) 34

…………….. ………….
N(8) 23
N(9) 55
N(10) 90

Array Elements

Once an array is created, we have to put something in it. Each element or index has an
integer value associated with it. Each data item of an array is called array elements. Each
array element is individually referred to by its subscript. The number of elements that an
array can store is calculated using the given formula.

Number of elements = (upper bound – lower bound +1)

Example: Declare a string array variable that can store address of any 10 students.

DIM add$(9)

Or

DIM add(9) AS STRING

Here, add$ is the name of string type array variable and 9 is subscript. Its upper bound
is 9. It can store any 10 string values from 0 to 4. The add$ array can store the ten string
values in the given format.

Add$ Add$ Add$ Add$ Add$ Add$ Add$ Add$ Add$ Add$
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9)

Pokhara Palpa Dhading Butwal Sarlahi Dhanusa Mugu Rolpa Sindhuli Parbat

Here the first element is represented by subscript 0. For example, Add$(0) = Pokhara
and the ninth element is represented by subscript 9 which is Add$(9) = Parbat. The upper

New Gateway to Computer Science Book -9 283

bound of this array is 9 and the lower bound of this array is 0. So this array can store only
(9-0+1=10) elements.

Option base statement: This statement defines the lower bound value for an array. This
statement should appear before the array in a program otherwise Qbasic displays error
message as ‘Duplicate Definition’. The general format of this statement is given below.

OPTION BASE n ,where n is a number whose value can be either 0 or 1.

Types of Array

There are various types of array but on the basis of number of data elements used,
the array can be classified into two types which we are going to discuss below.
 Single or one dimensional array
 Multi or two dimensional array

Single or One Dimensional Array

Single dimensional array represents a series of similar type of data represented by
a variable name with a single subscript. The data may be a series of numbers or
string arranged in a single row or column.

Let’s see the given table.
Elements of an array (N) N(1) N(2) N(3) N(4) N(5)

Values stored by an array 10 20 30 40 50

In the above table, an array variable (N) stores different numbers which contain the
number of variables which are called elements or subscripted variables. Likethis, N1, N2,
N3, N4, N5 are subscripted elements of variables of an array. It stores different values of
similar type.

Rules for declaring one dimensional array
 An array variable must be declared before being used in a program.
 Elements of an array must be of same type.
 An array index always starts from 0. For example, if an array variable is
declared as s[10], then it ranges from 0 to 9.
 Each array element is stored in a separate memory location.
 The name of array should be unique.
 It is not necessary to write a subscript as a constant.
 We can use variable and expression as its subscripts.

284 New Gateway to Computer Science Book -9

Practical Assignment

Program 1: Write a program to find the greatest number among any 10 different
numbers.
OPTION BASE 1
DIM N(10)
FOR I = 1 TO 10
INPUT "Enter a number"; N(I)
NEXT I
G = N(1)
FOR I = 2 TO 10
IF N(I) > G THEN G = N(I)
NEXT I
PRINT "The numbers entered by the users are:"
FOR I = 1 TO 10
PRINT N(I);
NEXT I
PRINT
PRINT "The greatest number is:"; G
END

Program 2: Write a program to find the sum of any 10 numbers.
CLS
DIM NUM(10)
FOR I = 1 TO 10
READ N(I)
S = S + N(I)
NEXT I
DATA 20, 30, 40, 50, 60
DATA 10, 15, 20, 25, 30
PRINT "The sum is"; S

END

New Gateway to Computer Science Book -9 285

Program 3: Write a program to store the marks of 5 subjects in an array.
OPTION BASE 1
CLS
DIM Marks(5)
FOR I = 1 TO 5
READ Marks(I)
PRINT Marks(I)
NEXT I
DATA 90, 85, 75, 65, 60
END
Program 4: Write a program to display the given output in the tabular form as given
below:
5 10 15 20
25 30 35 40
45 50 55 60
65 70 75 80
CLS
DIM N(16)
FOR I = 1 TO 4
FOR J = 1 TO 4
READ N(J)
PRINT N(J),
NEXT
PRINT
NEXT
DATA 5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80
END

286 New Gateway to Computer Science Book -9

Sorting Data in an Array

Sorting is the process of arranging data either in ascending or in descending order. It is
very important in programming. It can be applied both on string data or numeric data.
Let’s practise some programs for sorting data.

Practical Assignment

Program 5: Write a program to sort numbers in ascending order.
CLS
DIM N(15)
PRINT "The numbers entered by the users are::";
FOR I = 1 TO 15
READ N(I)
PRINT N(I);
NEXT I
DATA 15,11,17,19,14,22,45,35,10,9,7,46,32,77,89
FOR I = 1 TO 15
FOR J = 1 TO 15 - I
IF N(J) > N(J + 1) THEN SWAP N(J), N(J + 1)
NEXT J
NEXT I
PRINT
PRINT "The numbers in ascending order are::"
FOR I = 1 TO 15
PRINT N(I);
NEXT I
END

New Gateway to Computer Science Book -9 287

Program 6: Write a program to sort any five strings in ascending order.
CLS
DIM W$(5)
FOR I = 1 TO 5
INPUT "Enter the words"; W$(I)
NEXT I
FOR I = 1 TO 4
FOR J = I + 1 TO 5

IF W$(I) > W$(J) THEN SWAP W$(I), W$(J)
NEXT J
NEXT I
PRINT "The word in ascending order is as given below::"
FOR I = 1 TO 5
PRINT W$(I);
NEXT I

END

Searching Data in an Array

Searching is a process of finding the records required by the user. It is very easy to search
a record in the list of data stored in an array variable. We can search data by comparing
the data with each value stored in the array variable one after another in sequence. Let’s
practise some programs for searching data.

Practical Assignment

Program 7: Write a program to search data within an array.
CLS
DIM N(1 TO 5)
FOR I = 1 TO 5
READ N(I)
NEXT I
DATA 10,11,15,17,20
INPUT "Type the number to search"; N

288 New Gateway to Computer Science Book -9

FOR I = 1 TO 5
IF N = N(I) THEN
PRINT "The number is available on::"; I;
F=1

END IF
NEXT I
IF F = 0 THEN
PRINT "The number is not available"
END IF

END
Program 8: Write a program to search string data within an array. (ERROR)
CLS
DIM N$(5)
FOR I = 0 TO 5
READ N$(I)
NEXT I
DATA RAJU , RAM , KAMAL, DINESH, GOPAL SHYAM
FOR J = 0 TO 5
CH$ = LEFT$(N$(J), I)
IF UCASE$(CH$) = "D" THEN
PRINT N$(J)
END IF
NEXT J

END

Two Dimensional Arrays or the Matrix

The two dimensional array is also called a double dimensional array which is used for
representing the elements of the array in the form of the rows and columns and these
are used for representing the matrix. A Two Dimensional Array uses two subscripts for
declaring the elements of the Array. The double dimensional array is declared as:
DIM ArrayName(row,column) AS typeWhere, row and column specify the number of
rows and columns in the table.

Example

DIM D(3,4)

New Gateway to Computer Science Book -9 289

It defines a two dimensional array named ‘D’ having 3 rows and 4 columns. It has total 12
subscripted variables. The above example DIM D(3,4) displays the table as below.

Column 1 Column 2 Column 3 Column 4
D(1,4)
Frist Row D(1,1) D(1,2) D(1,3) D(2,4)
D(3,4)
Second Row D(2,1) D(2,2) D(2,3)

Third Row D(3,1) D(3,2) D(3,3)


Practical Assignment

Program 9: Write a program to display the output as below.
1 1
2 2
CLS
DIM vars(2, 2) AS INTEGER
vars(1, 1) = 1
vars(1, 2) = 1
vars(2, 1) = 2
vars(2, 2) = 2
FOR y = 1 to 2
FOR x = 1 to 2
PRINT vars(y,x),
NEXT x
PRINT
NEXT y
END
Program 10: Write a program to store data in the table.
CLS
OPTION BASE 1
DIM N(3, 4)
FOR I = 1 TO 3
FOR J = 1 TO 4

290 New Gateway to Computer Science Book -9

READ N(I, J)
NEXT J
NEXT I
DATA 10,11,12,13,14,15
DATA 16,17,18,19,20,21
REM DISPLAYING DATA IN TABULAR FORMAT
FOR I = 1 TO 3
FOR J = 1 TO 4
PRINT N(I, J),
NEXT J
PRINT
NEXT I
END
Program 11: Write a program to display the tabular form using multi- dimensional
array with their row sum.
CLS
DIM N(3, 3)
FOR I = 1 TO 3
FOR J = 1 TO 3
READ N(I, J)
NEXT J
NEXT I
DATA 2,4,5,3,2,1,7,8,3
FOR I = 1 TO 3
S=0
FOR J = 1 TO 3
PRINT N(I, J);
S = S + N(I, J)
NEXT J
PRINT "="; S
NEXT I
END

New Gateway to Computer Science Book -9 291

Program 12: Write a program to do the addition of a matrix.
CLS
REM Addition of Matrix.
INPUT "ENTER ROW"; R
INPUT "ENTER COLUMN"; C
DIM M1(R, C), M2(R, C)
PRINT "Enter Matrix M1"
FOR I = 1 TO R
FOR J = 1 TO C
INPUT M1(I, J)
NEXT J
NEXT I
PRINT " Enter Matrix M2"
FOR I = 1 TO R
FOR J = 1 TO C
INPUT M2(I, J)
NEXT J
NEXT I
FOR I = 1 TO R
FOR J = 1 TO C
M3(I, J) = M1(I, J) + M2(I, J)
NEXT J
NEXT I
PRINT "Addition of Matrix"
FOR I = 1 TO C
FOR J = 1 TO R
PRINT M3(I, J);
NEXT J
PRINT
NEXT I
END

292 New Gateway to Computer Science Book -9

Practical Section

Program 1: Type the following program in Qbasic and write the output in the given

box.

CLS

DIM X(5)

FOR I = 1 TO 5

X(I) = 2 * I

PRINT I; X(I)

NEXT I

END

Program 2: Write a program that allows the user to enter the various numbers and

display the sum of only an even digit.

CLS

INPUT "Enter the required numbers that you want to enter"; N

DIM NUM(N)

S=0

FOR I = 1 TO N

INPUT "Enter a Number"; NUM(I)

IF NUM(I) MOD 2 = 0 THEN

S = S + NUM(I)

END IF

NEXT I

PRINT "The sum of even numbers is:"; S

END

Program 3: Write a program to enter any ten numbers and display the

numbers in reverse order.

CLS

DIM n(10)

PRINT "Enter any 10 numbers"

FOR i = 1 TO 10

INPUT n(i)

NEXT

PRINT "The reverse order of number is"

New Gateway to Computer Science Book -9 293

FOR J = 10 TO 1 STEP -1

PRINT n(J);

NEXT

END

Program 4: Write a program to enter any five numbers and display the greatest
number among them.
CLS
DIM num(4)
PRINT "Enter any any 5 numbers"
FOR j = 0 TO 4
INPUT num(j)
NEXT j
L = num(0)
FOR j = 0 TO 4
IF L <num(j) THEN
L = num(j)
END IF
NEXT j
PRINT "largest number is="; L

END

Program 5: Write a program to enter any five numbers and display the smallest
number among them.
CLS
DIM num(4)
PRINT "Enter any any 5 numbers"
FOR j = 0 TO 4
INPUT num(j)
NEXT j
S = num(0)
FOR j = 0 TO 4
IF S >num(j) THEN
S = num(j)
END IF
NEXT j
PRINT "The smallest number is="; S

END

294 New Gateway to Computer Science Book -9

Program 6: Write a program to enter name and marks of any 5 students and display
with result either pass or fail.
CLS
DIM N$(5)
DIM M(5)
PRINT "Enter Name and Marks of any 5 students"
FOR I = 1 TO 5
PRINT I
INPUT "Enter Name: "; N$(I)
INPUT "Enter Marks:"; M(I)
NEXT I
PRINT
PRIN
PRINT
PRINT "MARKS OF STUDENTS"
PRINT "NAME", "MARKS", "RESULT"
FOR I = 1 TO 5
IF M(I) >= 40 THEN
R$ = "PASSED"
ELSE
R$ = "FAILED"
END IF
PRINT N$(I), M(I), R$
NEXT I

END

Program 7: Write a program to display the name and roll number of first 7 students.

DIM N$(6), roll(6)
CLS
FOR I = 0 TO 6
READ n$(I), roll(I)
NEXT I
FOR I = 0 TO 6
PRINT N$(I), roll(I)
NEXT I
DATA Dinesh,1, Kamal,2 ,Maya,3,Bhupendra,4,Divyanshi,5,Pranjita,6,Bikey,7

END

New Gateway to Computer Science Book -9 295

Assignment

1. Answer the following questions.
a. What is an array? What are the things that we have to indicate while declaring an
array?
b. What are the advantages of using an array?
c. What are the array variables?
d. What is an array element?
e. What are the types of an array? Define them.
f. What are the rules for declaring one dimensional array?
g. What is searching data in an array?

2. Debug the given programs.
a. DIM N(5, 10)
LET S = 0
FOR I = 0 TO 5
FOR J = 10 TO 1
LET S = S + N(I + 1) (J-1)
NEXT
NEXT
END

3. Write down the output of the given programs.
a. DIM N (5)
FOR I = 1 TO 5
READ N (I)
NEXT I
S = 0
FOR J = 1 TO 5
PRINT N (J)
S = S+N (J)
NEXT J
PRINT "SUM"; S
DATA 5, 10, 15, 20, 25
END

296 New Gateway to Computer Science Book -9

b. DIM N$(4)
FOR A = 1 TO 4
READ N$ (A)
NEXT A
FOR B = 1 TO 4
PRINT LEFT$(N$(B), B)
NEXT B
DATA ARUN, BARUN, KARUN, TARUN
END

5. Write array program for the followings.
a. A program to accept 15 numbers into an integer array and display odd or even
numbers.
b. To input 10 numbers and arrange them in an ascending order.
c. To input 20 numbers and arrange them in a descending order.
d. To display the largest number from the given data set.
45, 42, 56, 51, 48, 55, 96, 78, 22, 35,40,50
e. To display the smallest number from the given data set.
45, 42, 56, 51, 48, 55, 96, 78, 22, 35,40,50
f. Using single dimensional array display the given output.
10 15 20
25 30 35
40 45 50
g. To print the given table format.
1 3 5 7 = 16
9 2 4 3 = 18
8 6 5 4 = 23

h. To input any 15 numbers and display only the number that are divisible by
4.
i. To input the age of any 10 person and display the youngest one.
j. To read and print the 7 numbers in reverse order.

New Gateway to Computer Science Book -9 297

Program Writing Project

Program writing project to prepare sales bill report

CLS

INPUT "How many items to be calculated? ",totalItems%

DIM itemName$(1 TO totalItems%) ' Declare our arrays

DIM itemCost!(1 TO totalItems%)

DIM numItems%(1 TO totalItems%)

DIM totalCost!(1 TO totalItems%)

FOR i% = 1 TO totalItems% 'First loop: get inputs

CLS

PRINT "Item "; i% ' Display the current item number

PRINT

INPUT "Item name -- ", itemName$(i%)

INPUT "Item cost -- ", itemCost!(i%)

INPUT "Quantity --- ", numItems%(i%)

totalCost!(i%) = itemCost!(i%) * numItems%(i%)

NEXT i%

CLS

PRINT "Summary"

PRINT

format$ = "\ \ $$#,###.## #,### $$#,###,###.##"

PRINT "Item name Item Cost Quantity Total Cost "

PRINT "----------------- ---------- -------- --------------"

FOR i% = 1 TO totalItems%

PRINT USING format$; itemName$(i%); itemCost!(i%); numItems%(i%);
totalCost!(i%)

netTotal! = netTotal! + totalCost!(i%)

NEXT i%

PRINT

PRINT "Net Total = "; netTotal!

END

298 New Gateway to Computer Science Book -9

Specification Grid - 2077

Class - 9

Time: 1 Hour 30 Minutes Full Marks: 50

1×2 Type of Q. No. Area No. of Marks
Questions Questions
2
1 Subjective Group A 2
& (Computer Fundamentals) – 22 Marks 2
2 1.a 2
- Introduction to Computer 1×2 2
2
1.b -Types of computers 1×2 2
1.c - Computer System 1×2 2
2
1.d - Computer Hardware 1×2 2
1.e - Computer System 1×2 2
1.f - Working with Graphics 1×2
4
Objective 3 -Matching 4×0.5 2
4 - Multiple choice 4×0.5 2
5 - True&False 4×0.5 2
6 -Technical terms 4×0.5 2
7 -Full forms 4×0.5
(From the below chapters: 2
a) Introduction to Computer 2
b) Types of Computers 2
c) Computer System 2
d) Computer Hardware 2
e) Computer System 6
f) Working with Graphics )
50
3. Subjective Group B
(Internet & Web Technology) – 12 Marks

8.(a, b) -Internet Technology 2×2

9. a - HTML Tags (functions) 4×0.5
9. b - CSS Commands 4×0.5

Objective 10. a -Multiple choice/True and False 4×0.5
10. b - Technical terms/Matching 4×0.5

4. Subjective Group C
(Computer Programming) – 16 Marks

11. -Conceptual questions 2×1
(a,b,c) -Function and use of QBASIC 2×1
statements/functions
12 - Algorithm and Flowchart 1×2
13 -Debug a program 1×2
14 -Output of the given program code 1×2
15. a,b) - Write QBASIC program 2x3

Total 53

New Gateway to Computer Science Book -9 299


Click to View FlipBook Version