Practical Section
Program 1
Write a program to input any three numbers and display its sum and average.
REM ‘to calculate sum and average of three numbers
CLS
INPUT “Enter first number”; A
INPUT “Enter second number”; B
INPUT “Entre third number”; C
SUM = A + B + C
AVG = (A + B + C) / 3
PRINT “The sum is”; SUM
PRINT “The average value is”; AVG
END
Program 2
Write a program to display the area of a square.
REM program to display area of square
CLS
INPUT “ENTER LENGTH”; L
A=L^2
PRINT “AREA OF SQUARE”; A
END
Program 3
Write a program to display the perimeter of a rectangle.
REM program to display perimeter of rectangle
CLS
INPUT “ENTER LENGTH”; L
INPUT “ENTER BREADTH”; B
P = 2 * (L + B)
PRINT “PERIMETER OF RECTANGLE”; P
END
New Gateway to Computer Science Book -10 151
Program 4
Write a program to display the area and circumference of a circle.
CLS
LET PI = 3.14
INPUT “Enter radius of circle”; R
A = PI * R ^ 2
C = 2 * PI * R
PRINT “The area of the circle is”; A
PRINT “The circumference of the circle is”; C
END
Program 5
Write a program to exchange the value of any two variables.
CLS
INPUT “Enter the first number”; a
INPUT “Enter the second number”; b
PRINT “Before using SWAP”; a, b
SWAP a, b
PRINT “After SWAP”; a, b
END
Program 6
Write a Qbasic program to find out the area of four walls of a room.
CLS
Input “Enter the height”;H
Input “Enter the length”; L
Input “Enter the Breadth”;B
Let AR= 2 * H * (L+B)
Print “The area of four walls =”;AR
End
152 New Gateway to Computer Science Book -10
Program 7
Write a Qbasic program to enter any three numbers and calculate their sum and
average.
CLS
Input “Enter any number” ;A
Input “Enter any number” ;B
Input “Enter any number” ;C
Let Sum = A+B+C
Let Avg =Sum/3
Print “The sum=” ;Sum
Print “The Average is” ;Avg
End
Program 8
Write a Qbasic program to input student’s name, marks obtained in five different
subjects and find the total and average marks.
CLS
Input “Enter the name” ;N$
Input “Enter the marks in English” ;E
Input “Enter the marks in Computer” ;C
Input “Enter the marks in Maths” ;M
Input “Enter the marks in Science” ;S
Input “Enter the marks in Nepali” ;N
T=E+C+M+S+N
Let A=T/5
Print “The name of the student is” ;N$
Print “The total marks is” ;T
Print “The Average marks” ;A
End
Program 9
Write a Qbasic program to enter any number and check whether it is negative or
positive number.
CLS
Input “Enter the number”; N
New Gateway to Computer Science Book -10 153
If N>0 Then
Print “The number is positive”
Else
Print “The number is negative”
EndIf
End
Program 10
Write a Qbasic program to enter any number and find out whether it is even or odd
using the 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 11
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
Print “This is number 4”
154 New Gateway to Computer Science Book -10
Case 5
Print “This is number 5”
Case else
Print “Invalid number range “;
End select
End
Program 12
Write a QBASIC program to enter any alphabet and test 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 13
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
New Gateway to Computer Science Book -10 155
Program 14
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 15
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
Program 16
Write a QBASIC program to read any 10 different numbers and find out the
greatest one 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
156 New Gateway to Computer Science Book -10
Program 17
Write a QBASIC program to find 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 18
Write a QBASIC program to check whether the entered number is palindromic or
not.
CLS
INPUT “ENTER ANY NUMBER”; N
A=N
S=0
WHILE N <> 0
R = N MOD 10
S = S * 10 + R
N = N \ 10
WEND
IF A = S THEN
PRINT A; “This is a palindromic number.”
ELSE
PRINT A; “This is not a palindromic number.”
END IF
END
New Gateway to Computer Science Book -10 157
Program 19
Write a QBASIC program to check whether the entered number is prime or
composite.
CLS
INPUT “Enter any number”; N
A=0
FOR I = 1 TO N
IF N MOD I = 0 THEN A = A + 1
NEXT I
IF A = 2 THEN
PRINT “It is prime number”;
ELSE
PRINT “It is composite number”;
END IF
END
Program 20
Write a QBASIC program to display the prime numbers from 2 to 100.
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 21
Write a QBASIC program to display the Fibonacci sequence up to 10th terms.
CLS
A=1
B=1
158 New Gateway to Computer Science Book -10
PRINT A, B,
FOR I = 1 TO 10
C=A+B
PRINT C,
A=B
B=C
NEXT I
END
Program 22
Write a QBASIC program to check whether the entered number is Armstrong or
not.
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
New Gateway to Computer Science Book -10 159
Program 23
Write a QBASIC program to display the output as below.
1 CLS
222 FOR I = 1 TO 6
33333 LOCATE I, 50 - I
FOR J = 1 TO I * 2 - 1
4444444 PRINT USING “#”; I;
555555555 NEXT
666666666666 PRINT
NEXT
END
Program 24
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
Innovative Task
Prepare a chart paper including various QBASIC statements with their syntax and
submit to your teacher.
160 New Gateway to Computer Science Book -10
Assignment
1. Write down the function and syntax for the following statements.
a. INPUT statement b. PRINT statement
c. PRINT UNING statement d. LINE INPUT statement
e. READ…..DATA statement f. IF…….THEN statement
g. IF…….THEN……ELSE statement h. SELEST CASE statement
i. GOTO statement j. FOR….NEXT statement
k. WHILE…….WEND statement l. DO….WHILELOOP statement
m. DO………UNITL LOOP statement o. EXIT statement
2. Debug the given programs.
a. CLS
INPUT “Enter the name of student” ; n$
INPUT “Enter the marks of English”; e
INPUT “Enter the marks of Nepali” ; n
INPUT “Enter the marks of Computer”; c
TO = e + n - c
PER = e + n + c / 300 * 100
PRINT “Student’s name” ; n
PRINT “Total marks of student” ; TOTAL
PRINT “ Percentage of student”; PR
END
b. CLS
INPUT “Enter your choice (1 – 3)”; N
SELECT CASE Z
CASE 1
PRINT “Baishak”
CASE 2
PRINT “Jestha”
CASE 5
PRINT “Ashad”
New Gateway to Computer Science Book -10 161
ELSE CASE
PRINT “Invalid Value”
SELECT END
END
3. Write down the output of the given program.
a. CLS b. CLS
READ A, B, C, D x$ = “Ram”
Total = A + B +C + D y$ = “Krishna”
Avg = Total / 4 z$ = “Malakar”
PRINT “The total value is”; Total PRINT USING “!”; x$; “.”;
PRINT “The average value is”; Avg PRINT USING “!”; y$; “.”;
DATA 90,110,50,130 PRINT USING “&”;z$;
END END
c. CLS d. CLS
I=1 FOR I = 1 TO 6
While I < =10 LOCATE I, 50 - I
Print I^2; FOR J = 1 TO I * 2 - 1
I = I + 1 PRINT USING “#”; I;
WEND NEXT
END PRINT
NEXT
END
4. Study the given program and answer the given questions.
a. CLS
INPUT “Enter the first number”; a
INPUT “enter the second number”; b
PRINT “Before using SWAP”; a, b
SWAP a, b
PRINT “After SWAP”; a, b
END
i. If the user input the first number as 50 and second number as 70, what will be
the output?
162 New Gateway to Computer Science Book -10
ii. What are the variables used in the above program?
iii. What is the statement uses in the above program to exchange the value?
b. CLS
INPUT “ENTER LENGTH”; L
INPUT “ENTER BREADTH”; B
P = 2 * (L + B)
PRINT “PERIMETER OF RECTANGLE”; P
END
i. If the user inputs the length 150 and breadth 60, what will be the output?
ii. What are the operators used in the above program?
iii. List out the variables used in the above program.
5. Write a QBASIC program for the followings.
a. To input any three numbers and display its sum.
b. To display the area of a rectangle.
c. To display the area and perimeter of a rectangle.
d. To enter a number and check whether it is odd or even using select case statement.
e. To find the factorial of a given number.
f. To check whether the entered number is prime or composite.
g. To check whether the given number is divisible by 5 or not.
h. To check whether the given number is palindromic or not.
i. To display the Fibonacci sequence up to 20th term.
j. To check whether the given number is Armstrong or not.
k. To display the reverse of a given number.
l. To print the sum of square of odd numbers upto 200.
m. To input any number and count the total even numbers present in it.
6. Write the program to display the 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
New Gateway to Computer Science Book -10 163
Lesson
9 Modular Programming in Qbasic
Learning Outcomes
At the end of this lesson, students will be able to:
describe about modular programming.
define Qbasic programming and tell its features.
tell about the Qbasic operators.
explain the Qbasic statements with syntax.
tell work with the types of control statement.
write Qbasic programming using some control statements.
Modular programming can be used to break up a large program into small units,
or to create code that can be easily reused. A modular program consists of a main module
and one or more auxiliary modules. Each module originates from a separate source
code file. The main module is compiled as an EXE, and calls functions in the auxiliary
modules. Modular programming is a technique or method which is used to divide
a program into many small, logical and functional modules or blocks. In a QBASIC
program module is a block of statements that is used to solve a particular problem. The
following are the advantages of using modular programming. When a program becomes
too large then it becomes difficult to write and debug the program. Thus, modular
programming helps a programmer to write and debug easily.
Different programmers can design different program modules independently.
It is easy to code the program and testing is very easy.
A module can be used in multiple places, which reduces the program codes.
Debugging of the program is easier and faster because they are divided into different
modules.
It is possible to use a single module in different places which reduces program codes.
164 New Gateway to Computer Science Book -10
Parts of Module
A modular programming has two parts of modules they are main module and sub module.
Main Module: This module contains the entry point and the ending point of the program.
The top-level module which is located at the top of all procedures such as SUB-Procedures
or FUNCTION is called main-module. Procedure names and parameters are declared and
called from the main-module.
Sub Module: Sub module is a program which is written under the main module. A
program may have one or more sub modules under the main module. A sub-module may
have other sub-modules. It is also called a procedure. It is an isolated part of modular
program used for specific purposes. Sub module is controlled by the main module.
Main
Module
Sub Sub Sub
Module Module Module
1 2 3
Sub Sub Sub Sub Sub Sub
Module Module Module Module Module Module
1A 1B 2A 2B 3B 3B
Procedure and its Types
Procedure is a small, logical and manageable functional part of a program. It is a block
of statements that solves a particular problem given by the user. It is a self-contained and
independent manageable part of the program. A procedure is also known as a sub program
or a sub module contains codes for performing a special task.
Types of Procedures
Sub procedure
Function procedure
New Gateway to Computer Science Book -10 165
Understanding Sub Procedure
Sub procedure is a method to divide a large program into small programs. It is a small,
logical and manageable and functional part of a program that performs specific tasks and
does not return a value to the calling module. A sub procedure is needed to be declared
before it is used in a program. A CALL statement is used to call the sub procedure in a
program. It is written with SUB....END SUB statement. It has three parts, the first one
is the declaration part where we declare sub procedure, the second part is the main part
from where sub part is called and the third part is SUB part where we define our specific
tasks to do.
Features of sub procedure
Sub procedure does not return any value.
It does not have data type.
It always starts with the key word, SUB and ends with the keyword END SUB.
The parameters can be passed either by reference or by value.
To understand these 3 parts, look at the following example.
Example 1
DECLARE SUB SUM
CLS
CALL SUM
END
SUB SUM
Input “Enter the first number”; A
Input “Enter the second number”; B
S= A+B
Print “The sum is:” ; S
END SUB
In the above program we declared the sub procedure by using DECLARE keyword and
statements between CLS and END part. It is the main part where code runs and control
flow transfers to sub part by CALL keyword followed by sub name as a result the sub
part gets executed and we see output like (Enter the first number, ....). Again, when
SUB part ends then the control flow goes to the main part where we find END statement.
Finally, program is terminated. Let’s see another example of sub procedure. We can pass
value in sub procedure from the main part of the program by using parameters.
166 New Gateway to Computer Science Book -10
Example 2
DECLARE SUB AREA (L, B)
CLS
INPUT “Enter the length”; L
INPUT “Enter the breadth”; B
CALL AREA (L, B)
END
SUB AREA (L, B)
A=L*B
PRINT “The area is::”; A
END SUB
In the above program, two parameters are used. They are L and B which are enclosed with
small brackets as (L, B) in DECLARE line.
To create a sub procedure, we have to do three main things they are declaring a sub
procedure, defining sub procedure and calling a sub procedure.
Let’s Review
Modular programming is a technique or method which is used to divide a
program into many small, logical and functional modules or blocks.
In a QBASIC program module is a block of statement that is used to solve a particular
problem.
A modular programming has two parts of modules. They are main module and sub
module.
The top-level module which is located at the top of all procedures such as
SUB-Procedures or FUNCTION is called the main-module.
Procedure names and parameters are declared and called from the main-module.
A sub module is a program which is written under the main module.
A procedure is a small, logical and manageable functional part of a program.
There are two types of procedures. They are sub procedure and function
procedure.
A CALL statement is used to call the sub procedure in a program.
New Gateway to Computer Science Book -10 167
Declaring a SUB Procedure
Declaring a sub procedure means specifying the name of sub procedure, number of
arguments and parameters that they use. The syntax is given below.
Syntax: DECLARE SUB Name (Parameter List) Here, Name is the name of sub procedure
which you want to create. Parameter List indicates the number of arguments and its data
type which are used while creating the sub procedure. We always have to use parentheses
to enclose the parameters. This is also an optional list of variables.
In the above example 1, DECLARE SUB SUM where SUM is the name of the sub
procedure without parameter list. Like this in example 2, DECLARE SUB AREA (L, B)
where AREA is the name of the sub procedure with two parameters with argument types.
Defining a SUB Procedure
After the deceleration of sub procedure we have to define it. It is the main body part of the
sub procedure. The syntax is as below.
SUB sub procedure _name (Parameter lists)
Local variable and constant declaration
[Sub procedure statements]
END SUB
In the first line syntax given above, SUB is the QBASIC statement that makes the
beginning of the sub procedure definition. Sub procedure _name is the name of the sub
procedure which is used to call the sub procedure. We cannot use any keywords or the
same variable name or procedure name in the program. Parameter lists are the optional
list of variables which must be enclosed in parentheses.
In the second line syntax given above, Local variable and constant declaration are used
within only the sub procedure.
In the third line syntax given above, Sub procedure statements are the working part of the
sub program.
In the fourth line syntax given above, END SUB indicates the end of the sub program
definitions.
Calling a SUB Procedure
The call statement is used to call the sub procedure. It can be executed from anywhere
in the program by referring to the procedure name. This is known as calling a procedure.
The CALL statement is used to transfer control to another procedure. The syntax is given
below.
168 New Gateway to Computer Science Book -10
Syntax: CALL NAME (Parameter List) where, name is the name of the sub program that
is to be called and parameter list is the data that can be either variable or constant passed
to the sub programs parameter.
Example 3
A sub procedure to find the sum of any three numbers:
DECLARE SUB SUM (A, B, C) declaring sub procedure
CLS
CALL SUM calling sub procedure
END
SUB SUM defining sub procedure
INPUT “Enter the first number”; A
INPUT “Enter the second number”; B
INPUT “Enter the third number”; C
Arguments and Parameters
When a procedure is called from the main module, data or variables need to supply to the
procedure where they are needed to use. The arguments are the parameters that pass to
the procedure. The argument is also known as actual parameter and the parameter is also
known as formal parameter. The calling procedure can pass arguments to the procedure
in two ways. They are passing arguments by value and passing arguments by reference.
Passing Arguments by Value
When arguments are passed by value, it does not make any effect to the value of the
variables which are passed to a procedure as they are changed in the procedure. This
method is useful when we have to pass the same value to more than one procedure without
changing the values of the original variables in the calling program.
Example 4
DECLARE SUB SUM (A, B)
CLS
CALL SUM (50, 60)
END
New Gateway to Computer Science Book -10 169
SUB SUM (A, B)
S=A+B
PRINT “The sum is”; S
END SUB
In the example 4 above, 50, 60 are called and passed with the arguments A and B into the
sub procedure respectively.
Passing arguments by reference
When the argument is sent by reference, the address of each variable is copied to the
procedure’s variable and the changes made in the procedure’s variable will affect on
the variables used at the called program. Passing arguments by reference is the default
mode of passing argument. In this method, the address of the variables are passed to
the procedure which means the variable of procedure uses the memory location of the
argument which is passed to it.
Example 5
DECLARE SUB sum (A, B)
CLS
INPUT “Enter the first number”; x
INPUT “Enter the second number”; y
CALL sum(x, y)
END
SUB sum (A, B)
S=A+B
PRINT “The sum is”; S
END SUB
Differentiate between passing argument by value and passing argument by reference
Passing arguments by value Passing arguments by reference
When arguments are passed by value it When arguments are passed by reference
makes a duplicate copy of arguments and the address of the variables are passed to
their values are used directly in parameter. the procedure.
To pass the argument by value, variable is By default the value is passed by refer-
enclosed in parenthesis. ence.
It doesn’t make any effect on values of The changes made in the procedure’s vari-
variable which are passed to a procedure able will affect the variables used at call-
even they are changed in the procedure. ing module.
170 New Gateway to Computer Science Book -10
Parameters
A parameter represents a value that the procedure expects you to pass when you call it.
The procedure’s declaration defines its parameters. When you define a function or sub
procedure, you specify a parameter list in parentheses immediately following the procedure
name. For each parameter, you specify a name, data type, and a passing mechanism. The
calling code does not have to pass a value for it. The name of each parameter serves as a
local variable in the procedure. The way you use the parameter name is the same way to
use any other variable. Study the following program.
Example 6
DECLARE SUB SUM (A, B)
CLS
CALL SUM (50, 60)
END
SUB SUM (A, B)
S=A+B
PRINT “The sum is”; S
END SUB
Here, in this program the variables A and B are the parameters of sub module SUM and
the values 50 and 60 are the arguments. While invoking the module, the arguments are
passed to the sub module.
Types of Parameters
Formal Parameters
The variables which are used to specify or declare types of data to pass to the procedures
are formal parameters. In other words, formal parameters are those variables that are used
inside parenthesis immediately after the procedure name in the declaration part. A formal
parameter is always a variable. The name of each parameter serves as a local variable in
the procedure. This parameter receives a value sent by the actual parameters.
Actual or Real Parameters
Actual parameters are also called real parameters. These are the arguments which are
used to pass real value or data to the procedures. Actual parameters may be variables
or constant values. The data type of arguments and the formal parameters should match
according to the order they are passed to the procedure.
Example 7
DECLARE SUB SUM (A, B) [A and B are formal parameters]
LET X= 50
New Gateway to Computer Science Book -10 171
LET Y= 60
CALL SUM (X, Y) [X and Y are actual parameters]
END
SUB SUM (A, B)
S=A+B
PRINT S
END SUB
Example 8
DECLARE SUB interest (x, y, z) [Here x, y, z are the formal parameters]
CLS
INPUT “Enter principal amount”; p
INPUT “Enter time ratio”; t
INPUT “Enter rate”; r
CALL interest (p, t, r) [Here p, t, r are the actual parameters]
END
SUB interest (x, y, z)
I = (x * y * z) / 100
PRINT “Simple interest = “; I
END SUB
Scope of Variables
This variable type determines the size and layout of the variable’s memory. It also
determines the range of values which need to be stored inside that memory and nature
of operations that can be applied to that variable. Variable is an element of QBASIC
programming. A variable is a quantity whose value changes during the execution of a
program. It is a named location in the memory that is used to store a value during a
program execution. There are two categories of a variable. They are local variable and
global variable which we are going to discuss below.
Local Variable
A variable which is declared inside a module and cannot be accessed by other modules
is known as local variable. The value of local variable destroys when the program flows
out of procedures. The local variable can be used within the module where it is defined.
Local variable is the variable declared and used inside the procedure by default. Let’s
study the given program.
172 New Gateway to Computer Science Book -10
Example 9
DECLARE SUB SUM (A, B)
CLS
CALL SUM (50, 60)
END
SUB SUM (A, B)
S=A+B
PRINT “The sum is”; S
END SUB
In the above given program, ‘S’ is the local variable because the value of ‘S’ is limited to
be accessed in the sub module only.
Global Variable
Global variable is the variable that can be accessed from any procedure or module used
on the program. They are declared with the keyword SHARED, DIM SHARED,
COMMON SHARED. A variable declared in a COMMON or DIM statement with the
SHARED attribute is a global variable to module. Let’s study the given program.
Example 10
DECLARE SUB average ( )
DIM SHARED n(5)
CLS
FOR i = 1 TO 5
INPUT “Enter a number”; n(i)
NEXT i
CALL average
END
SUB average
FOR i = 1 TO 5
s = s + n(i)
NEXT i
a=s/5
PRINT “Average of numbers =”; a
END SUB
In the above given program, as the array variable n has been declared as global variable,
it is accessible in SUB procedure ‘average’ without passing n as parameter from calling
program.
New Gateway to Computer Science Book -10 173
Here, are some fundamental differences between Local and Global variables.
Parameter Local Global
Scope It is declared inside a function. It is declared outside the function.
Parameters Parameters passing is required Parameters passing is not necessary
for local variables to access the for a global variable as it is visible
value in other function. throughout the program.
Modification When the value of the local When the value of the global variable
of variable variable is modified in one func- is modified in one function, changes
value tion, the changes are not visible are visible in the rest of the program.
in another function.
Accessed by Local variables can be accessed You can access global variables by
with the help of statements, any statement in the program.
inside a function in which they
are declared.
DIM SHARED, SHARED and COMMON SHARED Statement
DIM SHARED statement is used to declare global variable in the main module.
Syntax: DIM SHARED [List of variables]
Example: DIM SHARED A
SHARED statement is used to declare global variable in the sub module. It is used to make
the variable accessible which is declared at the module level without passing them as
parameters.
Syntax: SHARED [List of variables]
Example: SHARED A
The COMMON SHARED is a non-executable statement that declares global variables
for sharing between modules (main program to sub program or function and vice versa).
This statement declares the variable as global so that all modules can use this variable.
Syntax: COMMON SHARED [List of variables]
Example: COMMON SHARED A
Example 11
A program to find the sum and difference of any two numbers using SHARED statement.
DECLARE SUB SUM () In this program, no any parameter is passed but the
DECLARE SUB Diff () variables A and B are shared in the both module
CLS by SHARED statement to calculate their values. A
INPUT “Enter the first no”; A and B are global variables in this program.
INPUT “Enter the second no”; B
174 New Gateway to Computer Science Book -10
CALL SUM
CALL Diff
END
SUB Diff
SHARED A, B
D=A-B
PRINT “The difference is”; D
END SUB
SUB SUM
SHARED A, B
S=A+B
PRINT “The sum is”; S
END SUB
Example 12
A program to reverse a word in the main module using COMMON SHARED statement
COMMON SHARED A$
DECLARE SUB Rev() In this program, the string variable A$ is not
CLS declared as parameter at the declaration part but its
INPUT “Enter any string”; A$ value is accessed in the modules so it is a global
CALL Rev(A$) variable.
END
SUB Rev(A$)
END
SUB Rev(A$)
FOR I = LEN(A$) to 1 Step-1
B$=B$ + MID$(A$,I,1)
NEXT I
PRINT “Reverse string is”; B$
END SUB
New Gateway to Computer Science Book -10 175
Let’s Review
Declaring a sub procedure means specifying the name of sub procedure, number of
arguments and parameters that they use.
After the deceleration of sub procedure we have to define it. It is the main body part of
the sub procedure
The CALL statement is used to call the sub procedure.
The CALL statement is used to transfer control to another procedure.
The arguments are the parameters that pass to the procedure.
The argument is also known as actual parameter and the parameter is also known as
formal parameter.
Passing arguments by reference is the default mode of passing argument.
A parameter represents a value that the procedure expects you to pass when you call it.
The two types of parameters are formal parameters and actual parameters.
The variables which are used to specify or declare types of data to pass to the
procedures are formal parameters.
Actual parameters are also called real parameters.
A variable is a quantity whose value changes during the execution of a program.
A variable which is declared inside a module and cannot be accessed by other
modules is known as local variable.
Global variable is the variable that can be accessed from any procedure or module
used on the program.
Understanding Function Procedure
Function procedure is a block of statement that performs specific task and return a value
whenever it is called. It is a small, logical and manageable functional part of a program
which performs specific tasks and returns single value to the main program or calling
module. It has three parts, first one is declaration part where we declare FUNCTION
procedure, second one is main part, from where FUNCTION procedure is called and
last one is FUNCTION part where we define our specific task to do. The syntax of this
procedure is given below.
Syntax:
DECLARE FUNCTION NAME [Parameter list]
[List of statements]
Function Name = Expression
[Statements…….]
END FUNCTION
176 New Gateway to Computer Science Book -10
Where, name is the name of the function-procedure which we want to create. Parameter is
a list of variables which indicates the number of arguments and its data types that is used
when calling the function. Expression is the return value of the function. Let’s understand
the parts of function procedure in the given example.
Example 13
DECLARE FUNCTION AREA (L, B) [Declaration Part]
CLS
INPUT “Enter the length”; L
INPUT “Enter the breadth”; B Main Module
AR = AREA (L, B)
PRINT “The area is”; AR Calling Function Procedure
END
FUNCTION AREA (L, B)
A=L*B Calling Function Procedure
AREA = A
END FUNCTION
In the above program, we declared the FUNTION procedure by using DECLARE keyword
and statements between CLS and END part. It is the main part where code runs and when
interpreter reads fifth line, flow of program jumps to the FUNCTION procedure. Inside
the function procedure, it calculates the area and returns the value in the main module.
After that it prints the area and when it reads END statements it terminates the program.
Difference between function procedure and sub procedure
Function Procedure Sub Procedure
A function must return a value. Sub procedure does not return value.
It is a block of code placed between SUB It is a block of code placed between
and END SUB statement. FUNCTION and END FUNCTION
statement.
A function is called by statement and Sub procedure is called by CALL
expression method. statement.
It does not have a data type It has a data type which is the return value
of the function
The print statement is mostly used in sub The print statement is mostly used in main
module. module.
New Gateway to Computer Science Book -10 177
Practical Section on SUB Procedure
Program 1 Program 2
Write a program to enter any two numbers
Write a program to enter any two numbers and display its difference.
and display its sum. DECLARE SUB DIFF (A, B)
DECLARE SUB SUM (A, B) CLS
CLS INPUT “ENTER FIRST NUMBER”; A
INPUT “ENTER FIRST NUMBER”; A INPUT “ENTER SECOND NUMBER”; B
INPUT “ENTER SECOND NUMBER”; B CALL DIFF (A, B)
CALL SUM(A, B) END
END SUB DIFF (A, B)
SUB SUM (A, B) D=A-B
S=A+B PRINT “DIFFERENCE”; D
PRINT “SUM OF TWO NUMBERS”; S END SUB
END SUB
Program 3 Program 4
Write a program to enter any two Write a program to find the area of four
numbers and display their average. walls.
DECLARE SUB AVERAGE (A, B) DECLARE SUB AREA (L,B,H)
CLS CLS
INPUT “ENTER FIRST NUMBER”; A INPUT “ENTER LENGTH” ; L
INPUT “ENTER SECOND NUMBER”; B INPUT “ENTER BREADTH” ; B
CALL AVERAGE (A, B) INPUT “ENTER HEIGHT” ; H
END CALL AREA (L,B,H)
SUB AVERAGE (A, B) END
AVG = (A + B) / 2 SUB AREA (L,B,H)
PRINT “AVERAGE OF TWO NUM”; AVG A=2*H*(L+B)
END SUB PRINT “AREA IS”; A
END SUB
178 New Gateway to Computer Science Book -10
Program 5 Program 6
Write a program to calculate the simple Write a program to calculate the area of a
interest. square.
DECLARE SUB SI(P,T,R) DECLARE SUB AREA (L)
CLS CLS
INPUT “ENTER PRINCIPLE” ; P INPUT “ENTER LENGTH”; L
INPUT “ENTER TIME” ; T CALL AREA(L)
INPUT “ENTER RATE” ; R END
CALL SI(P,T,R) SUB AREA (L)
END A=L^2
SUB SI(P,T,R) PRINT “THE AREA OF SQUARE IS”; A
I=(P*T*R)/100 END SUB
PRINT “THE INTEREST AMT IS ” ; I
END SUB
Program 7 Program 8
Write a program to convert Nepalese
Write a program to calculate the area of a currency into Indian Currency.
rectangle. DECLARE SUB CON(N)
DECLARE SUB AREA (L, B) CLS
CLS INPUT “ENTER NC ” ; P
INPUT “ENTER LENGTH”; L CALL CON(P)
INPUT “ENTER BREADTH”; B END
CALL AREA(L, B) SUB CON(P)
END C=P/1.6
SUB AREA (L, B) PRINT “INDIAN CURRENCY ”; C
A=L*B END SUB
PRINT “AREA OF RECTANGLE ”; A
END SUB
New Gateway to Computer Science Book -10 179
Program 9 Program 10
Write a program to calculate the
Write a program to calculate the area of perimeter of a rectangle.
a circle. DECLARE SUB PERI (L, B)
DECLARE SUB AREA (R) CLS
CLS INPUT “ENTER LENGTH”; L
INPUT “ENTER RADIUS”; R INPUT “ENTER BREADTH”; B
CALL AREA(R) CALL PERI(L, B)
END END
SUB AREA (R) SUB PERI(L, B)
A = 3.14 * R ^ 2 P = 2 * (L + B)
PRINT “AREA OF CIRCLE ”; A PRINT “THE PERIMETER IS”; P
END SUB END SUB
Program 11 Program 12
Write a program to calculate the area and
perimeter of a rectangle. Write a program to calculate the area and
DECLARE SUB AREA (L, B) perimeter of a square.
DECLARE SUB PERIMETER (L, B) DECLARE SUB AREA (L)
CLS DECLARE SUB PERIMETER (L)
INPUT “ENTER LENGTH”; L CLS
INPUT “ENTER BREADTH”; B INPUT “ENTER LENGTH”; L
CALL AREA (L, B) CALL AREA (L)
CALL PERIMETER(L, B) CALL PERIMETER(L)
END END
SUB AREA (L, B) SUB AREA (L)
A=L*B A=L^2
PRINT “AREA OF RECTANGLE ”; A PRINT “AREA OF SQUARE”; A
END SUB END SUB
SUB PERIMETER (L, B) SUB PERIMETER (L)
P = 2 * (L + B) P=4*L
PRINT “PERIMETER OF RECT ”; P PRINT “PERIMETER OF SQUARE ”; P
END SUB END SUB
180 New Gateway to Computer Science Book -10
Program 13 Program 14
Write a program to check whether the Write a program to display the prime
input number is prime or not. numbers from 1 to 100
CLS DECLARE SUB PRIME()
DECLARE SUB PRIME(A) CLS
CLS CALL PRIME
INPUT “ENTER A NUMBER” ; N END
CALL PRIME(N) SUB PRIME
END FOR I = 1 TO 100
SUB PRIME(A) C=0
FOR I = 1 TO A FOR J = 1 TO I
IF A MOD I = 0 THEN C=C+1 IF I MOD J=0 THEN C=C+1
NEXT I NEXT J
IF C=2 THEN IF C=2 THEN PRINT I;
PRINT “IT IS A PRIME NUMBER” NEXT I
ELSE END SUB
PRINT “IT IS NOT A PRIME NUMBER”
ENDIF
END SUB
Program 15 Program 16
Write a program that accepts three different Write a program to check whether the
numbers and display the smallest number. given number is positive or negative.
DECLARE SUB SMALL(A,B,C) DECLARE SUB TEST(N)
CLS CLS
INPUT “ENTER FIRST NUBER” ; A INPUT “Enter a number”; N
INPUT “ENTER SECOND NUMBER” ; B CALL TEST(N)
INPUT “ENTER THIRD NUMBER” ; C END
CALL SMALL(A,B,C) SUB TEST(N)
END IF N>0 THEN
SUB SMALL(A,B,C) PRINT N; “It is positive number.”
IF A<B AND B<C THEN ELSE
PRINT “THE SMALLEST NUMBER IS” ; A PRINT N; “It is negative number”
ELSE IF B<A AND A<C THEN END IF
PRINT “THE SMALLEST NUMBER IS” ; B END SUB
ELSE
PRINT “THE SMALLEST NUMBER IS” ; C
END SUB
New Gateway to Computer Science Book -10 181
Program 17 Program 18
Write a program to check whether the
input number is Armstrong or not. Write a program to input a string and
DECLARE SUB ARM (N) count the total numbers of vowels.
CLS DECLARE SUB COUNT (S$)
INPUT “ENTER ANY NUMBER”; N CLS
CALL ARM (N) INPUT “ENTER ANY STRING”; S$
END CALL COUNT(S$)
SUB ARM (N) END
A=N SUB COUNT (S$)
S=0 VC = 0
WHILE N < > 0 FOR I = 1 TO LEN(S$)
R = N MOD 10 B$ = MID$(S$, I, 1)
S = S + R ^ 3 C$ = UCASE$(B$)
N = N \ 10 IF C$ = “A” OR C$ = “E” OR C$ = “I”
WEND OR C$ = “O” OR C$ = “U” THEN
IF A = S THEN VC = VC + 1
PRINT A; “IT IS ARMSTRONG” END IF
ELSE NEXT I
PRINT A; “IT IS NOT ARMSTRONG” PRINT “TOTAL NUM OF VOWEL”; VC
END IF END SUB
END SUB
Program 20
Program 19
Write a program to display 2 3 5 8 12
Write a program to reverse the given ……upto 10th term.
string.
DECLARE SUB series ()
CLS
CLS
INPUT “ENTER A WORD” ; W$
CALL series
CALL REV(W$)
END
END
SUB series
SUB REV(N$)
I=2
FOR I = LEN(N$) TO 1 STEP-1
FOR J = 1 TO 10
C$=C$+MID$(N$,I,1)
PRINT I;
NEXT I
I=I+J
PRINT “REVERSED STRING IS” ; C$
NEXT J
END SUB
END SUB
182 New Gateway to Computer Science Book -10
Program 21 Program 22
Write a program to display the first Write a program to display the series
10 Fibonacci series numbers. Such as in the given pattern.
1,1,2,3,5,8,13…. 5
DECLARE SUB series () 5 5
CLS 5 5 5
CALL series 5555
END 55555
SUB series DECLARE SUB series ()
A=1 CLS
B=1 CALL series
PRINT A; END
PRINT B; SUB series
FOR I = 1 TO 10 N=5
C=A+B I=1
PRINT C; DO
A=B PRINT N
B=C N = N * 10 + 5
NEXT I I=I+1
END SUB LOOP WHILE I <= 5
END SUB
Program 23 Program 24
Write a program to display reverse of
Write a program to display the series as 2 input-string.
22 222 2222 22222 . DECLARE SUB REV(A$)
DECLARE SUB series () CLS
CLS INPUT “ENTER ANY STRING”;A$
CALL series CALL REV(A$)
END END
SUB series SUB REV(A$)
I=2 FOR I= LEN(A$) TO 1 STEP -1
FOR J = 1 TO 5 B$=MID$(A$,I,1)
PRINT I; W$=W$+B$
I = I * 10 + 2 NEXT I
NEXT J PRINT “REVERSED STRING=”W$
END SUB END SUB
New Gateway to Computer Science Book -10 183
Program 25 Program 26
Write a program to display the given series Write a program to ask a word in upper
pattern. case and convert it into lower case.
1 DECLARE SUB convt (W$)
22 CLS
333 INPUT “Enter a word in Uppercase”; W$
4444 CALL convt(W$)
55555 END
DECLARE SUB series () SUB convt (W$)
CLS R$ = LCASE$(W$)
CALL series PRINT R$
END END SUB
SUB series
FOR i = 1 TO 5
FOR j = 1 TO i
PRINT i;
NEXT j
PRINT
NEXT i
END SUB
Program 27 Program 28
Write a program to display the series in the
Write a program to display the series as 3
given pattern.
12 27 48 upto 10th terms.
1
121 DECLARE SUB series ()
12321 CLS
1234321
123454321 CALL series
12345654321 END
DECLARE SUB series ()
CLS SUB series
CALL series I=3
END FOR j = 1 TO 10
SUB series
FOR I# = 1 TO 6 PRINT I * j ^ 2;
A# = A# * 10 + 1 NEXT j
PRINT A# ^ 2
END SUB
NEXT
END SUB
184 New Gateway to Computer Science Book -10
Program 29 Program 30
Write a program to display the series in the Write a program to display the series in
given pattern. the given pattern.
1 12345
12 1234
123 123
1234 12
12345 1
123456 DECLARE SUB series ()
DECLARE SUB series () CLS
CLS CALL series
CALL series END
END SUB series
SUB series FOR I = 5 TO 1 STEP -1
FOR I = 1 TO 6 FOR J = 1 TO I
FOR j = 1 TO I PRINT J;
PRINT j; NEXT
NEXT PRINT
PRINT NEXT
NEXT END SUB
END SUB
Program 31 Program 32
Write a program to check whether a
Write a program to display the series as supplied number is perfect square or
3 12 27 48 upto 10th terms. not.
DECLARE SUB series () DECLARE SUB PSQR(N)
CLS CLS
CALL series INPUT “ENTER A NUMBER” ; N
END CALL PSQR(N)
SUB series END
I=3 SUB PSQR(N)
FOR j = 1 TO 10 S=SQR(N)
PRINT I * j ^ 2; IF S = INT(S) THEN
NEXT j PRINT “it is perfect square”
END SUB ELSE
PRINT “it is not perfect square”
ENDIF
END SUB
New Gateway to Computer Science Book -10 185
Program 33 Program 34
Write a program to input a string and Write a program to input string and display
display only the vowel letters. only the consonant letters.
DECLARE SUB VLETTER (S$) DECLARE SUB DISPC (S$)
CLS CLS
INPUT “ENTER ANY STRING”; S$ INPUT “ENTER ANY STRING”; S$
CALL VLETTER(S$) CALL DISPC(S$)
END END
SUB VLETTER(S$) SUB DISPC(S$)
FOR I = 1 TO LEN(S$) FOR I = 1 TO LEN(S$)
B$ = MID$(S$, I, 1) B$ = MID$(S$, I, 1)
C$ = UCASE$(B$) C$ = UCASE$(B$)
IF C$ = “A” OR C$ = “E” OR C$ = “I” IF C$ <> “A” AND C$ <> “E” AND C$ <>
OR C$ = “O” OR C$ = “U” THEN “I” AND C$ <> “O”AND C$ <> “U” AND
PRINT B$ C$ <> “ ” AND C$ <> “.” THEN
END IF PRINT B$
NEXT I END IF
END SUB NEXT I
Program 35 END SUB
Write a program to display the given Program 36
pattern.
L Write a program to check whether the
AL input number is even or odd.
PAL DECLARE SUB CONV(N)
EPAL CLS
NEPAL INPUT “ENTER A NUMBER ” ; N
DECLARE SUB pat () CALL CONV(N)
CLS END
CALL pat SUB CONV(N)
END IF N MOD 2 = 0 THEN
SUB pat PRINT “IT IS EVEN NUMBER”
A$ = “NEPAL” ELSE
FOR I = 1 TO LEN(A$) PRINT “IT IS ODD NUMBER”
PRINT RIGHT$(A$, I) ENDIF
NEXT I END SUB
END SUB
186 New Gateway to Computer Science Book -10
Program 37 Program 38
Write a program to display the given Write a program to display the given
pattern. pattern.
COMPUTER R
OMPUTER ER
MPUTER TER
PUTER UTER
UTER PUTER
TER MPUTER
ER OMPUTER
R COMPUTER
DECLARE SUB pat () DECLARE SUB pat ()
CLS CLS
CALL pat CALL pat
END END
SUB pat SUB pat
A$ = “Computer” A$ = “COMPUTER”
C = 20: S = 30 C = 20: S = 30
FOR I = 1 TO 8 FOR I = 1 TO 8
PRINT MID$(A$, I, C) PRINT RIGHT$(A$, I)
C = C - 2: S = S + 1 C = C - 2: S = S + 1
NEXT I NEXT I
END SUB END SUB
New Gateway to Computer Science Book -10 187
Program 39 Program 40
Write a program to display the hston(
Write a program to display the given ) to print 7 , 22 , 11 , 34 , 17 , 52 , 26 ,
pattern. 13 , 40 , 20 without passing parameters.
C DECLARE SUB number ()
CO CLS
COM CALL number
COMP END
COMPU SUB number
COMPUT a=7
COMPUTE FOR i = 1 TO 10
COMPUTER PRINT a;
DECLARE SUB pat () IF a MOD 2 = 0 THEN
CLS a=a/2
CALL pat ELSE
END a=a*3+1
SUB pat END IF
A$ = “Computer” NEXT
C = 20: S = 30 END SUB
FOR I = 1 TO 8
PRINT LEFT$(A$, I)
C = C - 2: S = S + 1
NEXT I
END SUB
Tips: Consider a number; if it is exactly divisible by 2 then write the quotient for the
next number. If it is not exactly divisible by 2 then multiply the number by 3 and add
1 which will be the next number. Repeat the same process for the second and so on.
Here, first number itself is 7.
7 = 3×7+1 = 11
2
22 = 11
2
22 = 11×3+1 = 34
2
34 = 17
2
17 = 17×3+1 = 52, So on.
2
188 New Gateway to Computer Science Book -10
Program 41
Write a program to enter the age of a person and display different messages using
the given conditions:
Age Message
< 15 Children
16-21 Adult
22-30 Matured
31- 44 Family Growing
>45 Old
DECLARE SUB age (A)
CLS
INPUT “Enter your age:”; A
CALL age(A)
END
SUB age (A)
SELECT CASE A
CASE IS < 15
R$ = “Oh ! I am children”
CASE 16 TO 21
R$ = “Hurray! I am adult now, Common!”
CASE 22 TO 30
R$ = “Now! I am matured, I have to care all.”
CASE 31 TO 44
R$ = “Yes! Yes! Yes! I have children now.”
CASE ELSE
R$ = “Alas! Now I am old.....hey god save me.”
END SELECT
PRINT R$
END SUB
New Gateway to Computer Science Book -10 189
Program 42
Write a program to input name of passenger and destination. Calculate total fare
using the following rates.
Destinations Rate per person
Dhading Bensi 180/-
Palpa 800/-
Pokhara 500/-
Malekhu 120/-
Also provide 5% discount if there are more then 10 passengers.
DECLARE SUB fare (p, d$)
CLS
INPUT “Enter number of passenger”; p
INPUT “Enter the destination to go”; d$
CALL fare(p, d$)
END
SUB fare (p, d$)
IF d$ = “Dhading Bensi” THEN
t = 140 * p
ELSEIF d$ = “Palpa” THEN
t = 190 * p
ELSEIF d$ = “Pokhara” THEN
t = 200 * p
ELSEIF d$ = “Malekhu” THEN
t = 80 * p
END IF
IF p >= 10 THEN d = 5 / 100 * t
f=t-d
PRINT “total bus fare is Rs:”; f
END SUB
190 New Gateway to Computer Science Book -10
Program 43 convert Program 44
Write a program to Write a program to enter any two numbers
hexadecimal into decimal. and display its sum, difference, product and
average.
DECLARE SUB Z(B$) DECLARE SUB SUM (A, B)
DECLARE SUB DIFF (A, B)
CLS DECLARE SUB PROD (A, B)
DECLARE SUB AVERAGE (A, B)
INPUT “ENTER HEXA VALUE”;B$ CLS
INPUT “ENTER FIRST NUMBER”; A
CALL Z(B$) INPUT “ENTER SECOND NUMBER”; B
CALL SUM (A, B)
END CALL DIFF (A, B)
CALL PROD (A, B)
SUB Z(B$) CALL AVERAGE (A, B)
END
FOR I=LEN(B$) TO 1 STEP -1 SUB SUM (A, B)
S=A+B
A$=MID$(B$,I,1) PRINT “SUM OF TWO NUMBERS”; S
END SUB
C=VAL(A$) SUB DIFF (A, B)
D=A-B
IF A$=”A” THEN C=10 PRINT “DIFFERENCE OF TWO
NUMBERS”; D
IF A$=”B” THEN C=11 END SUB
SUB PROD (A, B)
IF A$=”C” THEN C=12 P=A*B
PRINT “PRODUCT OF TWO NUM”; P
IF A$=”D” THEN C=13 END SUB
SUB AVERAGE (A, B)
IF A$=”E” THEN C=14 AVG = (A + B) / 2
PRINT “AVERAGE OF TWO NUM”; AVG
IF A$=”F” THEN C=15 END SUB
H=H+C*16^P
P=P+1
NEXT I
PRINT “DECIMAL VALUE IS”;H
END SUB
New Gateway to Computer Science Book -10 191
Program 45 Program 46
Write a program to convert decimal number Write a program to display the below
into binary. output.
DECLARE SUB A (N)
CLS * DECLARE SUB display ()
INPUT “ENTER A NUMBER”; N ** CLS
CALL A(N) *** CALL display
END **** END
SUB A (N) ***** SUB display
WHILE N <> 0 * FOR i = 1 TO 5
E = N MOD 2 ** FOR j = 1 TO i
B$ = STR$(E) *** PRINT “*”;
N = FIX(N / 2) **** NEXT j
C$ = B$ + C$ ***** PRINT
WEND * NEXT i
PRINT “BINARY EQUIVALENT IS”; C$ * FOR i = 1 TO 5
END SUB * FOR j = 1 TO i
* PRINT “*”;
* NEXT j
PRINT
NEXT i
FOR i = 1 TO 5
PRINT “*”
NEXT i
END SUB
192 New Gateway to Computer Science Book -10
Assignment
1. Answer the following questions.
a. What do you mean by modular programming?
b. What are the parts of module?
c. Define main module and sub module.
d. Define procedure and write its types.
e. Write down the features of SUB procedure.
f. Write down the syntax for declaring a SUB procedure and for Defining a SUB
procedure.
g. What is a parameter?
h. Define local and global variable.
i. Write differences between sub procedure and function procedure.
j. What is an argument? What do you mean by arguments passing by value and
passing by reference?
2. Write a Qbasic program for the following questions.
a. Write a program to declare a sub procedure to find the average of any three
numbers.
b. Write a program to enter any two numbers and display its sum, difference, product
and average.
c. Write a program to declare a sub procedure to find the greatest number among 5
different numbers.
d. Write a program to declare a sub procedure to find the cube of a given number.
e. Write a program to calculate the circumference of circle.
f. Write a program to convert the meter into kilometre.
g. Write a program to calculate the simple interest and total amount.
h. Write a program to find the perimeter of a rectangle.
i. Write a program to check whether the given number is odd or even.
j. Write a program to check whether the given number is exactly divisible by 3 and 5 or
not.
k. Write a program to check whether the given number is prime or composite.
New Gateway to Computer Science Book -10 193
l. Write a program to find the sum of the first ten natural numbers.
m. Write a program to check whether the entered letter is a vowel or consonant.
n. Write a program to display the area of four walls.
o. Write a program to check whether the given number is perfect cube or not.
p. Write a program to check whether the given number is perfect square or not.
q. Write a program to print the factorial of a given number.
r. Write a program to display the sum of individual digit. [756] [7+5+6 = 18].
s. Write a program to input a string and count the total numbers of vowels.
t. Write a program to check whether the given number is positive or negative.
u. Write a program to find the sum of natural numbers from 1 to 10.
v. Write a program to find the factors of any input number.
w. Write a program to check whether the input number is prime or not.
x. Write a program to display the prime numbers from 1 to 100.
y. Write a program to reverse a given string.
z. Write a program to check whether the input number is Armstrong or not.
3. Write a Qbasic program to display the given pattern.
a. 2, 4 , 6, 8 , 10 up to 100.
b. 3,6,9,12………20th terms
c. 1 , 11, 111, 1111, up to 5th terms.
d. 1, 4, 9, 16……..upto 10th terms.
e. 5, 55, 555, 5555, 5555
f. 100,99,98,97,96, ………….1
g. 99, 97, 95, 93, ……..20th terms.
h. 5, 10, 15, 20……10th terms.
i. 50, 45, 40, 35 ………5
j. 1 k. 3 l. 5
12 33 54
123 333 543
1234 3333 5432
12345 33333 5432
194 New Gateway to Computer Science Book -10
m. C n. EDUCATION o. COMPUTER
CO EDUCATIO OMPUTER
COM EDUCAT MPUTER
COMP EDUCA PUTER
COMPU EDUC UTER
COMPUT EDU TER
COMPUTE ED ER
COMPUTER E R
p. 30 q. 2 4 6 8 10
28 30 4 6 8 10
26 28 30 6 8 10
24 26 28 30 8 10
10
22 24 26 28 30
4. Write down the output of the given program.
a. DECLARE SUB OUTP () b. DECLARE SUB OUTP ()
CLS CLS
CALL OUTP CALL OUTP
END END
SUB OUTP SUB OUTP
A = 1 W$ = “DHADING”
B = 1 FOR I = 1 TO LEN(W$) STEP 1
DO PRINT LEFT$(W$, I)
PRINT A; NEXT
IF B MOD 3 = 0 THEN PRINT END SUB
B = B + 1
A = A + 1
LOOP UNTIL B > 9
END SUB
New Gateway to Computer Science Book -10 195
c. DECLARE SUB OUTP () d. DECLARE SUB pat ()
CLS CLS
CALL OUTP CALL pat
END END
SUB OUTP SUB pat
W$ = “DHADING” A$ = “COMPUTER”
FOR I = 1 TO LEN(W$) STEP 1 t = 5
PRINT RIGHT$(W$, I) FOR I = 1 TO LEN(A$) STEP 2
NEXT PRINT TAB(t); MID$(A$, t, I)
END SUB t = t - 1
NEXT I
END SUB
e. DECLARE SUB series () f. DECLARE SUB series ()
CLS CLS
CALL series CALL series
END END
SUB series SUB series
I = 3 FOR I = 1 TO 6
FOR j = 1 TO 10 FOR j = 1 TO I
PRINT I * j ^ 2; PRINT j;
NEXT j NEXT
END SUB PRINT
NEXT
g. DECLARE SUB series ( ) END SUB
CALL series
END h. DECLARE SUB PATTERN (S$)
SUB series CLS
X = 1 S$ = “NEPAL”
FOR K = 1 TO 4 CALL PATTERN(S$)
PRINT X; END
X = X + K SUB PATTERN (S$)
NEXT K FOR I = 1 TO LEN(S$)
END SUB PRINT TAB(I); MID$(S$, I, 1)
NEXT I
END SUB
196 New Gateway to Computer Science Book -10
i. DECLARE SUB PATTERN1 (S$)
DECLARE SUB PATTERN (S$)
CLS
S$ = “****”
CALL PATTERN(S$)
CALL PATTERN(S$)
CALL PATTERN1(S$)
END
SUB PATTERN (S$)
FOR I = 1 TO LEN(S$)
PRINT LEFT$(S$, I)
NEXT I
END SUB
SUB PATTERN1 (S$)
FOR K = 1 TO LEN(S$)
PRINT MID$(S$, K, 1)
NEXT K
END SUB
j. DECLARE SUB VLETTER (S$)
CLS
INPUT “ENTER ANY STRING”; S$
CALL VLETTER(S$)
END
SUB VLETTER(S$)
FOR I = 1 TO LEN(S$)
B$ = MID$(S$, I, 1)
C$ = UCASE$(B$)
IF C$ = “A” OR C$ = “E” OR C$ = “I” OR C$ = “O” OR C$ = “U” THEN
PRINT B$
END IF
NEXT I
END SUB
New Gateway to Computer Science Book -10 197
5. Study the given program and answer the following questions.
a. DECLARE SUB SUM (N) i. In the program how many times does the
FOR NEXT loop execute?
N = 5
CALL SUM (N) ii. Write the name of the procedure used in the
program.
END
SUB SUM (N)
FOR X = 1 TO N S = S + X iii. What are the operators used in the program?
NEXT X iv. What will be the output of the program?
PRINT S
END SUB
b. DECLARE SUB Stde (N$ U)
FOR Loop = 1 to 5
RED NM$ (Loop)
NEXT Loop
DATA RAM, PRATIM, PRASANT
DATA NISHA, RUDHAR
CALL Stde (NM$U)
END
SUB Stde (N$U)
Print “Name starting from P”
FOR J = 1 TO 5
C$ = MID$ (N$, (J), 1, 1)
If C$ = “P” THEN
PRINT N$ (J)
END IF
NEXT J
END SUB
i. List out the library functions used in the above program.
ii. List the conditional statements used in the above program.
iii. What are the string variables used in the above program?
iv. What will be the output of the given program?
198 New Gateway to Computer Science Book -10
c. DECLARE SUB EXAM (N$)
CLS
INPUT “Enter word”, WO$
CALL EXAM (WO$)
END
SUB EXAM (N$)
FOR I =1 TO LEN (N$)
PRINT RIGHTS (N$, I)
NEXT I
END SUB
i. Write the names of the two built in functions used in the above program.
ii. List the real parameter in the given program.
iii. Name the string variable used in the above program.
iv. What is the name of the looping used in the above program?
v. What is the function of the LEN(N$) in the above program?
d. DECLARE SUB ABC (S,Y,Z)
FOR P = 1 TO 3
READ A,B,C
CALL ABC (A,B,C)
NEXT P
DATA 5, 4, 3, 2, 1, 6, 7, 8,4
END
SUB ABC (X,Y,Z)
T=X+Y+Z
IF T MOD 2 = 0 THEN
PRINT T
END IF
END SUB
i. List any two variables used in the above program.
ii. How many time SUB-procedure will be called when the program is executed ?
iii. How many parameters are used in the above program?
New Gateway to Computer Science Book -10 199
e. DECLARE SUB SUM(N)
INPUT “Enter any number”; N
CALL SUM (N)
END
SUB SUM (N)
WHILE N <> 0: R=N MOD 10
S=S+R
N = N\10
WEND
PRINT “SUM”; S
END SUB
i. In which condition the statements within the WHILE….WEND looping statement
will not be executed?
ii. Will the output be same if we use “/” instead of “\” in the above program?
iii. List out the operators used in the above program?
iv. If a user enters the number 50, what will be the output of the above program?
f. DECLARE SUB WORD(N$)
CLS
N$=”PABSON”
CALL WORD(N$)
END
SUB WORD(N$)
B=LEN(N$)
C=1
WHILE C<B
M$=MID$(N$, B, 1)
PRINT M$
C=C+2
WEND
END SUB
i) List the string and numeric variables used in the program.
ii) What is the value of B in the program?
iii) What are the string functions used in the above program?
iv) List out the operators used in the above program.
200 New Gateway to Computer Science Book -10