Sie sind auf Seite 1von 37

1

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.

Nevertheless, there are four basic types of instruction namely:


1. Input output instructions
2. Arithmetic instructions
3. Branching instructions
4. Logic instructions

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

computer to change the sequence of execution of instruction, depending on


conditions built into the programme by the programmer.

TYPES OF PROGRAMMING LANGUAGE


The effective utilization and control of a computer system is primarily through
the software of the system. Just as system software directs the internal
operations of the computer while applications software allows the programmer
to use the computer to solve user made problems.

Programming languages which means the primarily means of human vs


computer communication which have evolved the early stage programmers
enter instruction into the computer in a language similar to that used in the
application software. Computer programming language can be classified into
the following categories:
i) Machine Language
ii) Assembly Language
iii) High level symbolic Language
iv) Very high level symbolic Language

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.

Assembly language is as well known as the low level programming language.


This allows the programmer to remember the operations codes easily than when
expressed strictly as binary numbers. It must be translated into a machine
language using a system software programme called an assembler. They are the
second generation computers languages.
High level language are machine independent, i.e. it can run on a variety of
computers. Its instructions do resemble English language statements and the
mathematical symbols are the symbols used in ordinary mathematics. Examples
are fortran, basic, cobol, pascal, algol, ada and pi/i. They are the third generation
computers language. Very high level language are the fourth generation
computers language that allow the user to specify what they require, rather, than
describing the procedures by which requirements are met. It also offers the user
3

an English-like set of commands and simple control structures in which to


specify general data processing or numerical operations.

PROGRAMMING MICROCOMPUTERS IN BASIC LANGUAGE


There are some computer languages which are capable of supporting both
scientific and business data processing efficiently and effectively. An example
of such computer language is basic.

Basic programming language is an easy- to-go use friendly language whose


instructions resembled elementary algebraic formular augmented by certain
English Language keywords such as input, read, print, Goto, if, then and so on.

WHAT DOES QBASIC STANDS FOR?


It is an acronyms for:
Q - Quick
B - Beginners
A - All-purpose
S - Symbolic
I - Instruction
C - Code

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.

A BRIEF HISTORY OF QBASIC


QBASIC came into existence in the year 1960s. It was developed by two
mathematical professors and technicians named Prof. John Kemeny and Prof.
Thomas Kurtz, at Dartmouth College, Hanover, NH 03755 USA (603) 646-1110
(E-mail): contact@dartmouth.edu

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.

On the contrary, QBASIC is a programming language invented and modified by


Bill Gates and Hitler designed to make a program that would launch missiles
from outer space in hopes of destroying the cancer that is killing /b/.
Unfortunately, since the earliest versions of QBASIC could only display text,
they had to settle for a word processing program that could WRITE SHIT ON
YOUR SCREEN instead.

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.

WHAT IS QBASIC PROGRAMME ALL ABOUT (AIMS / FOCUS OF


QBASIC)
1. It is a friendly people orientated easy to learn programme and fun to
use.
2. It is operated in an English-like commands
3. It is very powerful and popular- high level programming language.
4. It runs on all microcomputers.
5. It provides enough steps for coding a sample progrommes.
6. It solves practical mathematical equations applicable to scientist
mathematics.
7. It allow the user to write and play around QBASIC
8. Its operations share similar ideas on how to operates other basic programmes
such as visual basic, c plus, java, etc.

MORE ADVANCED ADVANTAGES OF PROGRAMMING LANGUAGE


5

1. It is friendly, that is, it is people-orientated. It is easy to learn and fun to use.


Any well organized person can learn to programme in BASIC.
2. It is very flexible, allowing the programmer to develop new programmes and to
alter existing programme with relatively little effort.
3. It is well suited for use in an interactive environment. This includes dedicated
microcomputer applications as well as large computer time sharing applications.
4. It is universally available, on both large and small computers. It has become the
standard programming language for most microcomputer applications.
5. The commonly used features of BASIC are relatively standard, though there are
some differences between one versions of Basic to another.

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

QBASIC SCREEN IDENTITY:


If you press the <Enter> key, you will be taken to the QBASIC HELP menu
where you find information that concerns QBASIC operations/statements.
While pressing the Esc key, the pop-up box will disappear and QBASIC
opens up blue painted environment.
6

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

TERMINOLOGIES OPERATED ON QBASIC PROGRAMME

1. PROGRAMMING A COMPUTER IN QBASIC


Programming a computer in QBASIC means that you tell the computer what
you want it to do. In other to tell the computer what to do in QBASIC, you need
those special words called commands. When you put all the needed/demand
special words in typing together in other to execute something, then it can be
regarded as coding. Therefore when the user writes or code a programme on
QBASIC is known as programming.

2. BASIC RESERVED WORDS


7

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.

3. Key Basic Statement


Key basic statement are commands or keywords required to type as you
code/write your programmes on the blue painted space which will later turn into
CAPITAL letters meant for execution of calculation, as well as running the
written/coded programmes.
In addition, each of the basic statement you type must be on a line and each line
must be assign, a number known as LINE number. These line numbers gives an
order of sequence how QBASIC should execute her programme. Usually a
positive multiples numbers from 0, - infinity are expected to used. This can be
optional to the user.

EXAMPLES OF KEY BASIC STATEMENTS OPERATED IN QBASIC


CODED PROGRAMMES

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.

6. Read Data Statement:


This statement allows information be followed by the word read while the
figures will be typed/supply at the same line typing of Data, before the final
answer be displayed on the V.D.U. Screen.

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

Dim means dimension. It is the statement used to reserved different locations of


stored values when many locations are involved in a programme by using just
one variable.

10. Colour Statement:


Colour is a statement used to change the colour appearance of the words on the
screen when you run the coded programme. This allows for fun, making sure
you add colour when the need arises.

Nevertheless, the colour list table is as follows:-


To Number to To paint Number to
paint used used
Black 0 Grey 8
Blue 1 Light blue 9
Green 2 Light green 10
Cyan 3 Light cyan 11
Red 4 Light red 12
Magenta 5 Light 13
Brown 6 magenta 14
White 7 Yellow 15
Bright white

11. Making Sound in QBASIC


Sound is an easy statement used to produce sound when you run your
programme. To use the keyword sound, you must type (enter) two (2) numbers.
The first number is the frequency of the sound in high or low capacity (kw)
which ranges from very low frequent (37kw) to (32767kw) very high frequency.
The second number is how long the sound should last.

12. Drawing Pictures/Objects or Shapes with QBASIC with:

i) The Screen Statement:


Before you even start drawing object, shapes or pictures, you have to tell
QBASIC that you want to draw pictures. To do this, you need to used screen
statement by assigning number to it. Number 12 (mode 12) is one of the screen
numbers that is used to draw pictures.
10

ii) To Draw Line(s):-


Line Statement is used to draw lines but you must specify by typing where the
line will starts and ends.

iii) Drawing a rectangular box in conjunction with line statement:


The line statement can also be used to draw other shapes for instance it can be
used to draw boxes filled with colours or empty.

iv) Drawing a Circle:


Another very important shape is circle. The keyword Circle can be used to draw
a circular shape but you also need to specify where the centre of the circle
should be and how far from the centre it will be drawn and to the edges it will
covers.

Watch all these statements when you start the practical.

ELEMENTS OF QBASIC

Every programming language consists of some important elements which are


required to make a programme be executed. Therefore elements are those typing
that are required to make a coded programme function properly, before running
it to see your answers. These elements can involves character sets, keywords,
constants, variables, operators and expressions.

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.

Examples of special symbols and signs used as a character set in QBASIC is


shown below:-
Signs Signs Signs Signs
+ > s
- < %
* , ( $
/ ; ) !
\ : . #
^ = ? Blank space

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.

In QBASIC coding of programme, constants are grouped into two broad


headings:
a) String Constant
b) Numeric Constant

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

Numbers written in thousands to QBASIC will not consists of separator (,)


which also can not be enclose in double quotes. Generally, mathematical and
logical operations can be done on them e.g. 101, 105.50, 720, 45603, etc.

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.

There are mainly two types of variables declaration. They are:-


a) String variable
b) Numeric 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.

Example of numeric variable declaration:


It can be expressed under four headings, namely, Integer, long integer, single
precision and double precision variable.

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

RULES FOR TYPING A VARIABLE TO QBASIC


1. Variable names typing can have maximum of 40 characters in length
typed together without space.
2. Variable names can have alphabets, numbers and alphanumeric on line
numbers.
3. Variable name must begin with a letter
4. Variable names cannot begin with fn or Fn alphabets for example, fnames$,
fnum, etc. It is because fn causes the to calculate frequency number of statistic
data. Dont use it as a variable name.
5. Variable names cannot be served words.
6. Variable names may be ended with type declaration characters like $, %,
&, !, and #

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

> Greater than A > B, CAT > RAT


< Less than A < B, CAT < CAT
>= Greater than or equal to A >= B, X$ >= Y$
<= Less than or equal to A <= B, X$ <= Y$
<> Not equal to A$ <> B$, X <> Y

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

Wel come Welcome


50 45 5045
The plus sign (+) is used as the string operator as the act of combining two
strings together which can also be known as as concatenation.

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.

Example of algebra expression:


It must be converted into QBASIC expression and interpretation before the
result is shown at the output.

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]

Example 2: 75/5^(3-1) Using the PEDMAS


Solve for bracket (parenthesis) (3 1) = 2
= 75/5^2
Solve for exponentiation (5^2) = 25
=75/25
Finally, solve for division =75/25
=3
Example 3:
In this example chose the student that solves his/her expression correct.
Student 1 student 2
3+4x2 3+4x2
=7x2 =3+8
= 14 = 11

Student 1 is wrong and student 2 is right in the order of operation.

LETS PRACTICE
17

COMMON PRACTICAL QUESTIONS AND ANSWERS ON HOW TO


CODE QBASIC PROGRAM

ALWAYS RUN YOUR PROGRAM (BY PRESSING F5 KEY) AT THE


END OF YOUR CODED PROGRAM TO QBASIC BLUE PAINTED
SPACE.

1. Write or code QBASIC program that demonstrates the steps or procedures


involve to prepare an office tea breakfast with the results shown at the output
screen.

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

2. Write a simple program that illustrate the below comment:

If Month = 11, 12, 1 or 2 (November, December, January, or February), then the


word, Cool Season appear on the screen. If month = 3, 4, or 5 (March, April,
or May), then the words Hot Season appear on the screen. If month is any
number from 6 to 10 (June to October ), then the words Wet Season appear on
the screen.

Solution:

DIM Month AS INTEGER


CLS
REM Program to predict season
PRINT
INPUT Enter the number for season as: , Month
SELECT CASE MONTH
CASE 11, 12, 1, 2
PRINT Cool Season
CASE 3 TO 5
PRINT Hot Season
CASE 6 TO 10
PRINT Wet Season
CASE ELSE
PRINT What kind of season is demanded
END SELECT

Alternatively:
3. Write a program that asks for a score then prints the grade assigned to the
score.

Solution:

DIM Score AS INTEGER


CLS
PRINT
19

INPUT "Enter The Test Score: ", Score


PRINT
SELECT CASE Score
CASE IS >= 97
PRINT "Grade A+"
CASE 93 TO 96
PRINT "Grade A"
CASE 90 TO 92
PRINT "Grade A-"
CASE 87 TO 89
PRINT "Grade B+"
CASE 83 TO 86
PRINT "Grade B"
CASE 80 TO 82
PRINT "Grade B-"
CASE 77 TO 79
PRINT "Grade C+"
CASE 73 TO 76
PRINT "Grade C"
CASE 70 TO 72
PRINT "Grade C-"
CASE 67 TO 69
PRINT "Grade D+"
CASE 63 TO 66
PRINT "Grade D"
CASE 60 TO 62
PRINT "Grade D-"
CASE ELSE
PRINT "Fail"
END SELECT

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

5. Write a program that display todays date in the date format of


dddd/mmm/yyyy

Solution:

CLS
INPUT Enter a year; year
INPUT Enter a day; day
INPUT Enter a month; month

PRINT The year is ; year


PRINT The month is ; month
PRINT The day is ; day
PRINT -or-
PRINT The day is; day; /; month; /; year
END
ALGEBRAIC EXPRESSION
6. How can the following formulas be converted to its QBASIC equivalent?

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)

TRIGONOMETRY / CONSTRUCTION / TRIANGLE QUESTIONS.


7. Write a program that calculate the area and circumference of a circle on
the following values for radius (12, 14, 15, 22 and 45 respectively).

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

(d) Make the calculation


Circumf = 2 * PI * radius
Area = PI * radius ^ 2

(e) Send the results to the screen for the user


COLOR 2
PRINT For a circle of radius; radius;
PRINT and using a value for PI as; PI
PRINT The calculated circumference is ; Circumf
PRINT The calculated Area is ; Area

(f) Stop

8. Write a program to express these equation

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

ADVANCED FEATURES TO CODE INTERVIEW PROGRAM

9. Write a program to illustrate an interview with the user to make the right choice
based on your instruction.

Solution:

10 REM Interview program


20 CLS
30 INPUT Please enter your first name; N$
40 INPUT Are you born again? Answer Y or N; A$
50 IF A$ = Y THEN 70
60 IF A$ = N THEN 90
70 PRINT N$, Youve made the right choice!
80 GOTO 100
90 PRINT N$, You need to get born Again
100 END

CODING LINEAR SIMULTANEOUS EQUATION ( THE ALMIGHTY


FORMULA)

10. Write a program to solve the below quadratic linear equation

x = -b b2 4ac
2a
Where 3x2 + 8x 16 = 0 find x.
24

Solution:

5 REM Program to evaluate quadratic formula


10 CLS
20 INPUT Enter the number of 1st coefficient of x; a
30 INPUT Enter number of second coefficient of x; b
40 INPUT Enter number of third coefficient of x; c
50 d = - b + SQR (b ^ 2 ( 4 * a * c))
60 e = 2 * a
70 f = - b SQR (b ^ 2 (4 * a * c))
80 g = d / e
90 h = f / e
100 PRINT
105 PRINT Result of positive x is ; g
110 PRINT Result for negative x is ; h
115 END

CODING A GAME GUESS PROGRAM ON QBASIC

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

PRINT Thanks for playing


END

CODING A RECTANGLE MEASUREMENT ON QBASIC

12. Given that:


5cm
6cm

Write a program to calculate the area of the rectangle drawn above

Solution:

REM Example 5.6


-------------------------------------------------------------
Compute the area of a rectangle
Length L and width W
--------------------------------------------------------------
DIM L AS SINGLE
DIM W AS SINGLE
DIM Area AS SINGLE
CLS
INPUT Length of the rectangle is ; L
INPUT Width of the rectangle is ; W
Area = L * W
PRINT The area of ; L; by; W; rectangle will be ; Area
END

USING BUSINESS / SMALL SCALE FIRM INCENTIVE TO CODE


QBASIC

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:

REM Program to set Canteen target


CLS
INPUT Target ; Target
INPUT Canteen Sales; Canteensales
IF Canteensales >= target THEN
Performance$ = We are Happy
Bonus = 100 + 0.01 * (canteensales target)
ELSE
Performance$ = We are NOT Happy
Bonus = 0
ENDIF
PRINT *=============================*
PRINT Target; Target
PRINT Sales; Canteensales
PRINT Performance; Performance$
PRINT Bonus; Bonus

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:

REM Canteen Management Incentive


CLS
INPUT Target ; Target
INPUT Sales; Canteensales
IF Canteensales >= 2 * target THEN
Performance = Excellent
Bonus = 1000
ELSEIF Canteensales >= 1.5 * target THEN
Performance$ = Fine
Bonus = 500
27

ELSEIF Canteensales >= target THEN


Performance$ = Satisfactory
Bonus = 100
ELSE
Performance$ = Unsatisfactory
PRINT Youre FIRED!!!
ENDIF
PRINT Target; Target
PRINT Sales; Canteensales
PRINT Performance; Performance$
PRINT Bonus; Bonus
28

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

18. Given the below arithmetic sequences:


Find the sum of 12 terms of the series:
7 + 11 + 15 +
Hence, the first term a = 7, common difference d = 4 and number of
terms n = 12.
Requirement:
Use QBASIC to write the above sequences and display the sum of the series at
the output.

Solution:
30

REM Arithmetic sequences


CLS
PRINT Enter first term
INPUT A
PRINT Enter common difference
INPUT D
PRINT Enter number of terms
INPUT N
PRINT The sequence is
T=A
S=O
FOR I% = 1 TO N
S=S+T
T=T+D
NEXT I%
PRINT T
PRINT The sum is ; S
END

CODING BANKING INTEREST/PENSION ADVANCE PAYMENT ON


QBASIC

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

DIM Years AS INTEGER


Dim Balance AS DOUBLE
Balance = 0

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

Interest = Interest / 100


Balance = Payment
DO WHILE Balance < Goal
Balance = Balance * ( 1 + Interest)
Years = Years + 1
LOOP
PRINT
PRINT You can retire in ; Years; years
PRINT with; Balance; in the ECO BANK
END
CODING A MENU LIST FOR CHOICE IN QBASIC

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:

20 = 2, 21 = 2, 22 = 4, 23 = 8, 24 = 16, 25 = 32, 26 = 64, 27 = 128, 28 = 256,


29 = 512, etc.

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

CODING ACCOUNTING RECORDS ON QBASIC

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:

i) Identity of the gate as G01


ii) Series of positive integers representing the income from the sales of ticket
at the gate

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:

5 REM Clear the screen


10 CLS
15 REM Enter the number of the gates that are available
30 INPUT Enter the number of the gates; P
35 DIM A$(P), S(P)
40 CLS
50 FOR K = 1 TO P
60 INPUT Please enter gate identity; A$(I)
65 REM Initialize S(I) which represents total income
70 S(I) = 0
80 INPUT Income from the gate : = ; C
85 REM Test for the negative income which is the terminator
90 IF C > 0 THEN
95 REM Add up the income for each gate
100 S(I) = S(I) + C
105 REM Pass the control to line 80
110 GOTO 80
115 REM Close the if statement
117 ELSE
119 GOTO 130
120 ENDIF
130 CLS
140 NEXT K
150 CLS
155 REM Create a for, next loop to print out the report
160 PRINT Gate ID. Total Income
180 FOR I = 1 TO P
190 PRINT A$(I),,, S(I)
210 NEXT I
215 REM Test for the gate with the largest income
35

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

24. Write QBASIC Program to read the records/accounts of 1000 employees of


STARBLISS (NIG.) LTD. Each record has the following layout:

Columns 1 20 contains employee surname,


Columns 21 35 contains employee other names,
Columns 36 45 contains employee department,
Columns 46 52 contains employee gross monthly income
Columns 53 54 contains spaces,
Columns 54 61 contains employee gross monthly deduction.

Requirement:
Calculate the total monthly net pay of each employee and generate the output
reports and accounts.

Solution:

10 REM This programme reads the records of 1000 employee of


20 STARBLISS (NIG.) LTD. Each employee is described by
30 surname, other names, departments,
40 gross monthly income and gross monthly deduction.
50 Calculate for each employee
60 the total gross income
65 the total gross deduction and
70 monthly net pay and print the output report
90 CLS
36

100 REM Set all previous key setting off


110 KEY OFF
120 DIM S$(1000), O$(1000), T$(1000), M(1000), U(1000), N(1000)
130 TINC = 0
140 TDED = 0
150 TNCTPAY = 0
160 REM Set a for and next loop to accept 1000 records
170 FOR I = 1 TO 1000
180 REM accept all the necessary data
190 INPUT Surname: ; S$(I)
200 INPUT Other names: ; O$(I)
210 INPUT Department: ; T$(I)
220 INPUT Gross monthly income: ; M(I)
230 INPUT Gross monthly deduction: ; U(I)
240 REM Calculate monthly net pay for each employee
250 N(I) = M(I) U(I)
260 TINC = TINC + M(I)
270 TDED = TDED + U(I)
280 TNETPAY = TNETPAY + N(I)
290 CLS
300 NEXT I
310 CLS
320 REM Print the required report
330 FOR I = 1 TO 1000
340 PRINT S$(I), O$(I), T$(I), M(I), U(I), N(I)
350 NEXT I
360 REM Print total gross income, total gross deduction and total net pay
370 PRINT Total gross income: ; TINC
380 PRINT Total gross deduction: ; TDED
390 PRINT Total net pay: ; TNETPAY
400 END

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

Das könnte Ihnen auch gefallen