Professional Documents
Culture Documents
INTRODUCTION TO
COMPUTER PROGAMMING IN QBASIC LANGUAGE
OVERVIEW:
There are two facts that dive human reasoning and thoughts. Basically, human
beings cannot speak or write in computer language and computers cannot speak
or write in human language, therefore an Intermediate language had to be
developed to allow people to communicate with the computers. These
intermediate languages are known as programming language which allows
computer programmers (Users) to direct the activities of the computer. Thus,
programming languages are structured around unique set of rules that direct the
computer to perform a specific task. A typical instruction in a programming
language consists of two parts:-
1. An operation code
2. Some operands
The operation code tells the computer what to do such as add, subtract, multiply
and divide. The operands tells the computer the data items involved in the
operations. E.g. considering the instruction a = b + 5, the = and + are
operation codes while a, b and 5 are operands. Some computer uses many
types of operation codes in their instruction format and may provides several
methods for doing the same thing.
Summary:-
An input instruction directs the computer t accept data from a specific input
device and store it in a specific location. An output instruction tells the
computer to move a piece of data from a computer storage location and record it
on the output screen. All of the basic arithmetic operations can be operated by
the computer. Branch instruction causes the computer to alter the sequence of
execution of instruction within the programme. Logic instructions allow the
2
Summary:-
Machine language programmes allows the programmer to take advantage of all
the features and capabilities of the computer system for which it was designed.
A machine language is computer dependent. Thus, it is the first generation
computers language.
DEFINITION:
QBASIC is one of the many versions or dialects of the basic language which
retains the user friendly environment characterizes by the implementation of
basic language. This means that QBASIC runs on the windows to facilitate the
storing of coded programmes in the secondary memory for retrieval with the
ability to support real integer and string data typed. It also contains necessary
tools that are required for the manipulation of matrices, graphs and images.
The story goes that the students were having difficulty learning FORTRAN so
the professors decided that creating a language from scratch would be an
4
excellent teaching tool for the students. The result was BASIC, which was
eventually placed in the public domain.
Bill Gates and Paul Allen had something different in mind. In the 1970's when
M.I.T.S.'s Altair personal computer was being conceived Allen convinced Gates
to help him develop a Basic Language for it. When M.I.T.S. answered with
interest, The future of BASIC and the PC began. Gates was attending Harvard at
the time and Allen was a Honeywell employee. Allen and Gates licensed their
BASIC to M.I.T.S. for the Altair. This version took a total of 4K memory
including the code and data used for a source code. Gates and Allen then ported
Basic to other various platforms and moved back to their hometown of Seattle
where they had attended grade school together. It was at this time that the
Microsoft Corporation began it's reign in the PC world.
LOADING QBASIC
Loading QBASIC is very easy depending on your computer. It is commonly
found in all computers except vista. But one can purchase it from CD ROM and
install it.
Method: 1
Loading through Ms. DOS
Click start button
Click run
Type cmd and select Ok
Type cd\ and press enter
Type qbasic and press enter key ones
Note:
If Ms. DOS display invalid command, it means QBASIC is not installed on
your computer. But if QBASIC screen pop-up appear, two choices will be
shown.
Alternatively:
Method: 2
Loading through Windows Desktop
Double click on QBASIC or SHORTCUTS TO QBASIC
Double click QBASIC and choose to run if available
In addition, one will find the menus of QBASIC display after the title bar.
QBASIC MENU
Diagram:
Below the menu is the blue painted area called the process. QBASIC process is
the desktop where the user operates the demand and coded programmes but the
user will not see the outcome / result displayed. It is expected that the user sees
the outcome of his coded programme on the Black & White screen called the
output.
Down below the process screen (blue painted) space is the line of Immediate
IMMEDIATE LANE:
Diagram:
The immediate line cannot process any data, therefore it is no go area.
Nothing works there. It is useless going there to type or code your program.
OUTPUT SCREEN
Diagram:
At the output screen the user is prompt to press any key to return back to the
process when the user have finished viewing the outcome of his/her programme
via the message Press any key to continue
These are commands or keywords that acts in the capacity of commands and
execution, when typed. QBASIC uses it to execute your written programmes.
For instance, some acts in the capacity to calculates, to show final results, to
correct errors, to allow writing of numbers or words to be a heading, etc.
On the contrary, these reserved words only acts as commands and execution but
in terms of typing your programmes, do not use those reserved words, because
if typed it will cause your programmes written on the process (blue painted
space) to malfunction.
1. Rem Statement:
Rem is a special keyword known as remark. Sometimes when a programmer
write long programme, he or she will like to leave notes inside their programme
so that they can remember why they did certain things. So to leave a note that
explains certain things, use remark. QBASIC knows that when it sees Rem it
does not have to do what you wrote after it on that line.
2. Cls Statement:
Cls means clear screen. If you run a programme from QBASIC, everything that
was on the screen before you started QBASIC will show (i.e. written on the
screen), that is why we need the keyword cls.
3. List Statement:
8
List statement is used to examine the programme and correct errors before it is
executed. This is because QBASIC displays and execute programmes line-by-
line according to the numbers used.
4. Print Statement:
Print is one of the most useful and powerful commands in QBASIC that causes
the information typed between the double quote ( ) to be sent to the screen or
visual display unit end result where the users sees or runs the programme to get
the final results.
In addition, when Print is typed on the line number without double quote (i.e.
line number bearing print), QBASIC will provide one empty space between
such line and the next line at the output.
5. Input Statement:
This statement allows information to be typed using the keyboard of the
computer in generating the needed data before the final answer will appear at
the output. The computer executes this input statement by using a representative
question mark ( ? ) waiting for the data (i.e. figures) at the output screen to be
typed in.
7. Let Statement:
This statement will cause the arithmetic expression on the right hand side typed
before an equal sign ( = ) be stored as the result (answer that belongs to) the
variable at the left of the equality sign.
8. End Statement:
This statement stops the execution of the programme. That is to say, end
statement tells the computer that the written/coded programme done on
QBASIC blue painted space (process) has come to an end.
9. Dim Statement:
9
ELEMENTS OF QBASIC
CHARACTER SETS:
These are typing required when coding a programme which can be type in
alphabets both in smaller and capital letters, numbers that ranges 0 to 9, and
11
infinity. Special symbols and signs are also part of character set in which each
performs meanings and various functions and makes your program of standard.
CONSTANTS
They are the data or the values stored inside variable coded on qbasic program
that cannot changed during the program execution except for numbers. The data
may be a letter, words, numbers, special symbols but they are stored inside a
variable name which QBASIC will not changed the way you type it, except it is
numbers. Why? Because numbers when stored as constants, QBASIC can
perform mathematical calculation on it.
String Constants:
It is a letter, words, numbers, combination of letters with numbers or special
symbols enclosed in double quotes ( ) stored inside a variable name in which
mathematical operations cannot be done on it. It can also be used to express
statement on a program typed to QBASIC. For instance, B, Apple, Symbol
No. 10208, Welcome to QBasic World!!!, etc.
Numeric Constants:
Numeric constant refers to a number with or without decimal point, stored
inside a variable name in which mathematical operations can be done on it.
12
VARIABLE:
In QBASIC, a variable is a place on the computer memory which has a name
and stores data temporarily. On the otherhand, a variable is an entity that stores
data needed to be used in a program. Each program defines different number of
variables. The value of a variable can be change during the execution of the
program. On declaring (i.e. typing) a variable, one will need to determine the
type of data which QBASIC will stored inside the variable.
String Variable:
String variable stores string data (i.e. quotation statements) with a string
variable name declare on that same line whereby mathematical and logical
operations cannot be done on it as directed by the declaration symbols of dollar
sign ($).
Numeric Variable:
A numeric variable stores number data inside it variable name that mathematical
and logical operations can easily be done on it as directed by its declaration
symbols typed.
For integers, it stores short whole number with the sign of percentage (%) typed
to the variable name. Long integer stores large range of whole numbers with
ampersand (&) declaration, for single precision, it stores single decimal
numbers with or without declaration of exclamation mark (!) and for double
precision, it stores large decimal numbers declares with the use of hash sign (#)
13
OPERATORS
Operators are symbols that indicate the type of mathematical operations
QBASIC has to perform on the data or values of the variable. In QBASIC there
are four types/branches of operators used on coded program namely:
Arithmetic, Relational, Logical and String operators.
Arithmetic operators:
These are all the general mathematical signs used to perform simple and
practical mathematical calculations like addition, subtraction, division,
multiplication, exponents, etc
Example 1:
Operation Operators Example Results
Addition + 5+8 13
Subtraction - 86 2
Multiplication * 5*4 20
Division / 8/2 4
Integer division \ 9\2 4
Exponential ^ 4^3 64
Modular division Mod 7 Mod 3 1
Relational Operators:
They are operators used to perform comparison on two values of the same typed
statements. E.g.
Operators Relation Examples
= Equal to A = B, A$ = B$
14
Logical Operators:
It combine two or more relational expression to evaluate a singe value as true
(non zero answer) or false (zero answer). For example:
And operation:
When all the results (statements) returned are true.
Statement 1 (for P) Statement 2 (for Q) Results (P & Q)
F T F
T F F
F F F
T T T
Or operation:
It return true if any one of the relational statements expressions return true. E.g.
Statement 1 (for A) Statement 2 (for B) Results (A & B)
F T T
T F T
T T T
F F F
Not Operation:
It operates on one statement operand and returns true if the logical statement
returns false. E.g.
Statement 1 (for A) Result (for Not A)
F T
T F
STRING OPERATOR
String joins two or more than two string data (sentences) together to form a
simple or compound sentences.
E.g.
String data (for A$) String data (for B$) Results (A$ + B$)
15
EXPRESSION
It is the combination of operators, constants and variables that is evaluated to
get a result. The result is string data, numeric data or logical value and can be
stored in a variable. E.g.
(A + B) > C
A >= B + C
u = t + etc
Note, expression can come from any side of the mathematics, some from linear
simultaneous equation, algebra, statistics, trigonometry, etc.
A=LXB A=L*B
P = 2(L+B) P = 2 * (L + B)
I = (P X T X R)
100 I = (P * T * R)/100
3
V = 4 pi r
3 V = 4/3 * (PI * R ^ 3)
V = r h
2
V = PI X r ^ 2 * h
ORDER OF OPERATION
The order of operations is the order (due process) in which all algebraic
expression should be simplified.
For instance on QBASIC:
Parenthesis
Exponents (and Roots)
Multiplication and Division
Addition and Subtraction
[PEMDAS]
16
Alternatively:
Brackets
Exponentiation
Division and Multiplication
Addition and Subtraction
[BEDMAS]
Note:
Be aware that multiplication does not always come before division, and addition
does not always come before subtraction. For example.
3X35+2
Your working will always starts from left to right.
You work like this. First notice that there is no parenthesis or exponents, so we
move to multiplication and division. Theres only one multiplication, so we do
that first and end up with 9 5 + 2
Now we move to addition and subtraction, so finally we do the subtraction to
get 4 + 2 and finally the addition to give 6. Thus the order becomes:
[PEDMSA]
LETS PRACTICE
17
Solution:
5 REM Example 1
10 REM Coding a program that illustrate
15 REM how to prepare office tea breakfast
18 CLS
20 LET A$ = Wash the kettle
25 LET B$ = Add clean water into the kettle
30 LET C$ = Heat the kettle for 15 minutes
35 LET D$ = Check if the water is boiling
40 LET E$ = If the water boils after 15 mnutes
45 LET F$ = Add coffee, sugar and milk
47 PRINT
49 PRINT Directives to prepare office tea
50 PRINT ..
53 PRINT
55 PRINT The first procedure is to ; A$
60 PRINT The next procedure is to ; B$
63 PRINT The next procedure is to ; C$
65 PRINT The next process is to ; D$
68 PRINT The next process is to ; E$
70 PRINT The last procedure is to ; F$
75 END
18
Solution:
Alternatively:
3. Write a program that asks for a score then prints the grade assigned to the
score.
Solution:
4. Write a program with the comment: I am 11 years old, soon I will grow
up and be 25.
Solution:
20
CLS
AGE = 11
PRINT I am; AGE; years Old.
AGE = 25
PRINT But I will grow and soon will be ; AGE
END
Solution:
CLS
INPUT Enter a year; year
INPUT Enter a day; day
INPUT Enter a month; month
a) a = 3+ 3
4+4
E.g.
LET a = (3+3)/(4+4)
b) x = (a b)(a c)2
E.g.
LET x = (a b) * (a c) ^ 2
21
1
/3
c) f = a
b
E.g.
Let f = a ^ ( 1 / 2 ) / b ^ ( 1 / 3 )
d) d = (8 x2) - (4 x 2 1)
(x 9) x3
E.g.
Let d = ((8 x ^ 2) / ( x 9) (( 4 * 2 1) / (x ^ 3)
Solution:
REM
Author: Sir Isaac
Class: Computer science 404
Date: 07/09/2011
Purpose:
This program will calculate the Area and circumference of a circle
after receiving the value for the radius from the user.
(a) Start
CLS
(b) Declare variable, define known values
DIM radius AS DOUBLE Radius
DIM PI AS DOUBLE Pi
DIM Circumf AS DOUBLE Circumference
DIM Area as DOUBLE Area
PI = 3.141593
22
COLOR 9
(c) Get Required data from user
PRINT This program calculates the Area and Circumference of a circle
INPUT What is the radius of the circle; radius
(f) Stop
y = a(b c)2
d+2
where a = 2, b = 8, c = 4 and d = 2
Solution:
5 REM
10 CLS
15 a = 2
20 b = 8
25 c = 4
30 d = 2
35 e = a * (b c) ^ 2
40 f = d + 2
23
45 y = e / f
50 PRINT Your answer is ; y
55 END
9. Write a program to illustrate an interview with the user to make the right choice
based on your instruction.
Solution:
x = -b b2 4ac
2a
Where 3x2 + 8x 16 = 0 find x.
24
Solution:
11. Write a program to play a game of knowing how many days are there in a
week?
Solution:
CLS
Score = 0
PRINT How many days are there in a week?
INPUT a
IF a = 7 THEN GOTO 2
PRINT Wrong answer!
PRINT To try again press y
INPUT a$
IF a$ = y THEN GOTO 1 ELSE END
Score 10
PRINT Its the right answer!
PRINT Your Score is now; Score; !
25
Solution:
13. You have a Canteen Business and you want to create an incentive bonus for
your staff that meet the stipulated target. Write a program that illustrate these
statement.
26
Solution:
14. Alternatively, the Canteen management held meeting and agreed that where the
incentive to the staff does not meet the target, there will be redundancy. Re-
write the program to show the decision of the management.
Solution:
15. Write a program to select days of the week within seven days.
Solution:
CLS
INPUT Date Number is ; dayNumber
SELECT CASE dayNumber
CASE 1
PRINT Monday
CASE 2
PRINT Tuesday
CASE 3
PRINT Wednesday
CASE 4
PRINT Thursday
CASE 5
PRINT Friday
CASE 6
PRINT Saturday
CASE 7
PRINT Sunday
CASE ELSE
PRINT How many days in your week?
END SELECT
MATHEMATICAL EXPRESSION
16. Write a programme that will show a list of times table number of 3s e.g.
3 x 1 = 3, 3 x 2 = 6, etc.
Solution:
5 CLS
10 REM Compute times table numbers of 3
15 INPUT Enter any number for ; n
20 FOR I = 1 to 12
29
25 PRINT n; x; I = n * I
30 NEXT I
35 END
17. Write a program that generates multiples of 5 between 5 and 500, displaying
their sum and products at the output screen.
Solution:
5 CLS
10 REM Program to generate multiples of 5
15 between 5 and 500
20 Calculates their sum and products
30 displayed their results
40 SUM = 0
50 PRDT = 1
60 FOR L = 5 TO 500 STEP 5
70 SUM = SUM + L
80 PRDT = PRDT * L
85 Display the current values of L, SUM AND PRDT
90 PRINT L, SUM, PRDT
100 NEXT L
110 display the final values of L, SUM and PRDT
120 PRINT L, SUM, PRDT
130 END
Solution:
30
19. Given that the goal you set for your retirement in your current employer is
N850,000. You decide to make deposit of your pension payment in advance to
ECO BANK PLC with payment of N1,000 each month with the interest of 5.0%
interest charge. Write a program to illustrate the above comments, showing the
number of years it will take to complete the retirement task.
Solution:
CLS
REM Program to meet pension retirement goal
DIM Goal AS DOUBLE
DIM Interest AS DOUBLE
DIM Payment AS DOUBLE
31
PRINT So you want to put some money aside for retirement, hmmmm.
INPUT How much money do you need to retire; Goal
INPUT How much money will you contribute every year; Payment
INPUT Interest rate in % (e.g. use 5.0 for 5.0%) will be; Interest
20. Write a menu list program that allow the user to make a wish/choice in a
statistical analysis coding system.
Solution:
CLS
PRINT Which Statistical Analysis do you wish
PRINT
PRINT 1. Compute an Average
PRINT 2. Compute a Standard Deviation
PRINT 3. Find the Median
PRINT 4. Find the Smallest and Largest data value
PRINT 5. Plot the Data
PRINT
PRINT 0. Quit/Return to preview menu
32
DO
INPUT Enter your choice ( 0 through 5); a%
Use SELECT CASE or IF to check the user
Input, and do the work that has been requested.
LOOP UNTIL a% = 0
MATHEMATICAL CONTINUES
21. Suppose we are given ten rectangles and required to find the area of each
rectangle. Given below is a QBASIC program that reads the length and breath
of each rectangle, finds its area and prints the areas.
Solution:
5 CLS
10 REM Program that reads the length L and
15 breath B of ten rectangles
20 calculates the area of each rectangle
30 and prints the result
40 KANTER = 0
50 PRINT Please enter the values of L and B
60 INPUT L
65 INPUT B
70 Area = L * B
80 PRINT L, B, Area
90 KANTER = KANTER + 1
100 IF KANTER > 10 THEN 120
110 GOTO 50
120 END
DATA:
6 X 5cm, 3 X 4cm, 16 X 6cm, 4 X 5cm, 6 X 8cm, 7 X 3cm, 10 X 4cm, 2
X 2cm, 9 X 2cm, 12 X 6cm.
22. Given that you are required to calculate the powers of numbers. Write a
programme that list all the resulting powers of an integer that is less than 1000.
33
For example:
Solution:
CLS
REM Compute List of powers
Const Maxpower = 1000
DIM Power AS INTEGER
DIM n AS INTEGER
Power = 1 The first power n ^ 0 is always 1
COLOR 10, 1
PRINT This program prints all powers <; Maxpower; of an integer
INPUT Enter an integer for; n
DO WHILE Power < Maxpower
PRINT Power;
Power = Power * n
LOOP
23. Below is the record of Sales on tickets from each gate of a sports stadium
represented as a set of data in the following form:
Sample Records
G01 27.00 254.00 15.00 21.00
G02 2.00 196.00 240.00
G03 25.00 300.00 10.00
G04 36.00 298.00 150.00 etc
34
Write a QBASIC programme to read the sample data above, calculate the total
income for each gate separately and calculate the total income for the stadium
and the gate which records the largest income.
Solution:
217 LG = 0
220 FOR N = 1 TO P
230 IF S(I) > LG THEN
240 LG = S(I)
250 T$ = A$(I)
260 ENDIF
270 NEXT N
275 REM Print the gate with the largest income and the amount
290 PRINT The gate with the largest income; LG, is; T$
320 END
Requirement:
Calculate the total monthly net pay of each employee and generate the output
reports and accounts.
Solution:
25. Write QBASIC Programme to input the records of a file where each record
contains the identity of a person, the amount of loan in Naira he obtains (the
principal), the interest rate of the loan and the number of payment period per
year. The programme should compute the loan payment required for each pay
37
period to repay the outstanding principal in one year at the indicated rate in
Naira equal payments. The formula for calculating payments is:
Payment = P(1 + K) + 12
n
Where
P = Principal loan
K = Interest rate
n = Number of periods per year
Solution:
10 CLS
20 REM Set previous key setting off
30 KEY OFF
40 REM Enter the number of the debtors
50 INPUT Please enter the number of debtors to consider; d
60 CLS
70 DIM A$(d), B$(d), C(d), D(d), E(d), F(d)
80 REM Create a for and next loop to accept the records of debtors
90 FOR I = 1 TO d
100 INPUT Surname: ; A$(I)
110 INPUT Other names: ; B$(I)
120 INPUT Principal Loan: ; C(I)
130 INPUT Interest rate: ; K(I)
140 INPUT Payment period per year = ; E(I)
150 REM Compute the payment per period for each debtor
160 F(I) = (C(I) * (1 + K(I)) + 12) / E(I)
170 CLS
180 NEXT I
190 CLS
200 REM Print the report
210 FOR I = 1 TO d
220 PRINT A$(I), B$(I), C(I), K(I), E(I), F(I)
225 NEXT I
230 END