Main Intro To QBASIC
Main Intro To QBASIC
Computers can perform different tasks. In school, students use computers for tasks such as writing papers,
searching for articles, sending email, and participating in online classes. At work, people use computers to
analyze data, make presentations, conduct business transactions, communicate with customers and coworkers,
control machines in manufacturing facilities, and do many other things. At home, people use computers for tasks
such as paying bills, shopping online, communicating with friends and family, and playing computer games. And
don’t forget that cell phones, iPods, BlackBerries, car navigation systems, and many other devices are computers
too. The uses of computers are almost limitless in our everyday lives. Computers can do such a wide variety of
things because they are programmed to do so. This means that computers are not designed to do just one job,
but to do any job that their programs tell them to do. By definition, a computer system is a device that accepts
input known as data from the user, process the inputs based on a set of instructions called program and
produce the results or output also called information.
A program is a set of instructions that a computer follows to perform a task, for example, Microsoft Word and
Adobe Photoshop. Microsoft Word is a word processing program that allows you to create, edit, and print
documents with your computer. Adobe Photoshop is an image editing program that allows you to work with
graphic images, such as photos taken with your digital camera. These Programs are commonly referred to as
software. Software is essential to a computer because it controls everything the computer does. All of the
software that we use to make our computers useful is created by individuals working as programmers or
software developers. A programmer, or software developer, is a person with the training and skills necessary to
design, create, and test computer programs. Computer programming is an exciting and rewarding career. Today,
you will find programmers’ work used in business, medicine, government, law enforcement, agriculture,
academics, entertainment, and many other fields. This course introduces you to the fundamental concepts of
computer programming using the beginners’ language known as BASIC programming language.
OVERVIEW OF PROGRAMMING
Computer programming is a problem-solving approach. Basically, programmers develop programs when
encountered with challenges of proffering solutions to real life problems. A computer program has immense
potential for saving time/energy, as most computational tasks that are repetitive or can be generalized can be
done by computer programs. Programs can be built using programming languages which are classified into three
categories viz;
a) Machine language: This is the only language the computer understands and is also called a binary language. It
is made up of 0’s and 1’s.
b) Low Level languages: These are assembly languages that use mnemonics to represents computer operations.
The mnemonics are translated into machine language using a translator program known as the assembler. An
Assembly language is this symbolic language used to enter machine code instructions using easy-to-remember
mnemonics. The assembler converts assembly language statements into machine codes.
c) High Level Languages (HLL): These are computer languages that allow the programmer or software developer
to develop program codes using human understandable expressions. HLL uses an intermediate translator
program known as Compiler or interpreter to converts program codes written in HLL to machine understandable
0’s and 1’s for the computer to execute the programs. Examples include; BASIC, Java, C++, VB .Net, python,
Pascal, FORTRAN, PHP, etc.
This course intends to introduce students to computer programming using high level programming languages.
We shall adopt the beginners programming language known as BASIC (Beginner’s All-Purpose Symbolic
Instruction Code) to get students acquainted with computer programs. The Basic programming language is
interpreted and therefore requires an interpreter program in order to run any program written in the Basic
programming language.
ALGORITHMS
Recall that programming is a problem-solving approach. To develop programs that will solve human problems
efficiently, there is a need for a step-by-step representation of the solutions before transforming them into
computer programs using specific computer languages. An algorithm is a representation of a solution to a
1
problem. It is procedure for solving a problem in finite number of steps. Algorithms provide step-by-step
methods of accomplishing a task.
The term algorithm originally referred to any computation performed via a set of rules applied to numbers
written in decimal form. The word is derived from the phonetic pronunciation of the last name of Abu Ja'far
Mohammed ibn Musa alKhowarizmi, who was an Arabic mathematician who invented a set of rules for
performing the four basic arithmetic operations (addition, subtraction, multiplication and division) on decimal
numbers.
An algorithm is procedure consisting of a finite set of unambiguous rules (instructions) which specify a finite
sequence of operations that provides the solution to a problem, or to a specific class of problems for any
allowable set of input quantities (if there are inputs). In other word, an algorithm is a step-by-step procedure to
solve a given problem Alternatively, we can define an algorithm as a set or list of instructions for carrying out
some process step by step.
In the problem-solving phase of computer programming, you will be designing algorithms. This means that you
will have to be conscious of the strategies you use to solve problems in order to apply them to programming
problems. These algorithms can be designed or implemented using flowcharts or pseudocode.
FLOWCHARTS
Flowcharting is a tool developed in the computer industry, for showing the steps involved in a process. A
flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows - each shape
represents a step in the process, and the arrows show the order in which they occur. Flowcharting combines
symbols and flow lines, to show figuratively the operation of an algorithm.
Flowcharting Symbols
There are 6 basic symbols commonly used in flowcharting: Terminal, Process, input/output, Decision, Connector
and Predefined Process. This is not a complete list of all the possible flowcharting symbols; it is the list of ones
used most often in structured programming.
2
Flow Lines Shows direction of flow
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no other entry points. The exit point for
all flowchart symbols is on the bottom except for the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides or the bottom and one side.
4. Generally a flowchart will flow from top to bottom. However, an upward flow can be shown as long as it does
not exceed 3 symbols.
• From the bottom of the page to the top of the same page.
6. Subroutines and Interrupt programs have their own and independent flowcharts
7. All flow charts start with a Terminal or Predefined Process (for interrupt programs or subroutines) symbol. 8.
All flowcharts end with a terminal or a contentious loop.
Flowcharting uses symbols that have been in use for a number of years to represent the type of operations
and/or processes being performed. The standardized format provides a common method for people to visualize
problems together in the same manner. The use of standardized symbols makes the flow charts easier to
interpret; however, standardizing symbols is not as important as the sequence of activities that make up the
process.
Example 1: Design an algorithm and the corresponding flowchart for adding the tests cores as given below:
a) Algorithm
1. Start
2. Sum = 0
6. Add to sum
8. Add to sum
3
11. Get the fifth testscore
16. Stop
Start
Sum=0
4
A
Print sum
The algorithm and the flowchart above illustrate the steps for solving the problem of adding six test scores.
Where one test score is added to sum at a time. Both the algorithm and flowchart should always have a Start
step at the beginning of the algorithm or flowchart and at least one stop step at the end, or anywhere in the
algorithm or flowchart. Since we want the sum of six test scores, then we should have a container for the
resulting sum. In this example, the container is called sum and we make sure that sum should start with a zero
value by step 2.
Example 2: The problem with this algorithm is that, some of the steps appear more than once, i.e. step 5 get
second number, step 7, get third number, etc. One could shorten the algorithm or flowchart as follows:
1. Start
2. Sum = 0
3. Get a value
5. Go to step
7. Stop
5
Start
Sum=0
Get a value
output
stop
This algorithm and its corresponding flowchart are a bit shorter than the first one. In this algorithm, step 3 to 5
will be repeated, where a number is obtained and added to sum. Similarly, the flowchart indicates a flow line
being drawn back to the previous step indicating that the portion of the flowchart is being repeated. One
problem indicates that these steps will be repeated endlessly, resulting in an endless algorithm or flowchart. The
algorithm needs to be improved to eliminate this problem. In order to solve this problem, we need to add a last
value to the list of numbers given. This value should be unique so that, each time we get a value, we test the
value to see if we have reached the last value. In this way our algorithm will be a finite algorithm which ends in a
finite number of steps as shown below. There are many ways of making the algorithm finite.
The new list of numbers will be 26, 49, 498, 9387, 48962, 1, -1. The value –1 is a unique number since all other
numbers are positive.
1. Start
2. Sum = 0
3. Get a value
8. Stop
Corresponding flowchart:
6
Start
Sum=0
Get a value
Print sum
Start
PSEUDOCODE
Pseudocode is one of the tools that can be used to write a preliminary plan that can be developed into a
computer program. Pseudocode is a generic way of describing an algorithm without use of any specific
programming language syntax. It is, as the name suggests, pseudo code —it cannot be executed on a real
computer, but it models and resembles real programming code, and is written at roughly the same level of
detail.
Pseudocode, by nature, exists in various forms, although most borrow syntax from popular programming
languages (like C, Lisp, or FORTRAN). Natural language is used whenever details are unimportant or distracting.
In the algorithm design, the steps of the algorithm are written in free English text and, although brevity is
desired, they may be as long as needed to describe the particular operation. The steps of an algorithm are said
to be written in pseudocode. Many languages, such as Pascal, have a syntax that is almost identical to
pseudocode and hence make the transition from design to coding extremely easy. Examples of pseudocodes
include; suppose you are required to design an algorithm for finding the average of six numbers and the sum of
the numbers is given. The pseudocode will be as follows:
Start
Average = sum / 6
Stop
This is the pseudo-code required to input three numbers from the keyboard and output the result.
Print sum
7
End program
The following pseudo-code describes an algorithm which will accept two numbers from the keyboard and
calculate the sum and product displaying the answer on the monitor screen.
>= 80 Distinction
>= 60 Merit
>= 40 Pass
< 40 fail
The pseudo-code is
PROGRAMMING PARADIGMS
WHAT IT IS: The word paradigm is used a great deal when talking about programming languages. What does it
mean?
A programming paradigm is a style or “way” of programming. Some languages make it easy to write in some
paradigms but not others.
Declarative — Programs state the result you want, not how to get it.
Functional (Applicative) — Computation proceeds by (nested) function calls that avoid any global state.
Object-Oriented — Computation is affected by sending messages to objects; objects have state and behavior.
o Class-based — Objects get their state and behavior based on membership in a class.
Logic (Rule-based) — Programmer specifies a set of facts and rules, and an engine infers the answers to
questions.
Constraint — Programmer specifies a set of constraints, and an engine infers the answers to questions.
INTRODUCTION TO QBASIC
1. It is a user-friendly language.
2. It is widely known and accepted programming language.
3. It is one of the most flexible languages, as modification can easily be done in already existing program. 4.
Language is easy since the variables can be named easily and uses simple English phrases with mathematical
expressions.
RULES OF QBASIC
Every programming language has a set of rules that have to be followed while writing a program; following are
some rules of QBASIC language:
1. All QBasic programs are made up of series of statements, which are executed in the order in which they are
written.
2. Every statement should have at least one QBasic command word. The words that BASIC recognizes are called
keywords.
3. All the command words have to be written using some standard rules, which are called “Syntax Rules”. Syntax
is the grammar of writing the statement in a language. Syntax Errors are generated when improper syntax is
detected.
4. Basic program files must be saved with a file extension name of .bas. For a QBASIC program to run, it must be
saved.
DATA TYPES
Data is a collection of facts and figures that is entered into the computer through the keyboard. Data is of two
types:
1. CONSTANT: Data whose value does not change or remains fixed. A constant is a data object whose value
cannot be changed. There are two types of constants:
(a) NUMERIC CONSTANT: Numbers -negative or positive used for mathematical calculations e.g. –10, 20, 0
(b) ALPHANUMERIC CONSTANT / STRING: Numbers or alphabets written within double quotes (inverted commas
“ “). e.g. “Computer”, “Operating System”
2.VARIABLE: A variable is a data object whose value can be defined and redefined. A variable can simply be
referred to as a name which can contain a value. It is a data whose value is not constant and may change due to
some calculation during the program execution. A variable is actually a location in the computer’s memory,
which stores the values. Depending on what value is held, Variables are of two types:
9
NUMERIC VARIABLE: The variable that holds a Numeric data values for arithmetic calculations (+, - ,*, / )
is called a Numeric Variable. e.g. A = 50, here A is the Numeric Variable. In numeric variables, the symbol
% means integer and it truncate real numbers eg: A% = 6.2 implies A%=6 A%=6.6 implies A% = 7 etc. the
symbols !, & and # represents single, long and double precision variables respectively.
CHARACTER OR STRING VARIABLE: The variable that holds an Alphabetic, Alphanumeric or numeric
data value, which cannot be used for arithmetic calculations, is called Character Variable or String
Variable. This variable must end with a $ sign and the value it hold must be enclosed in inverted
commas. e.g. Name$ = “BringBackOurGirls”, here Name$ is a string Variable. If a variable holds a symbol
or text, it may be a character variable or a string variable. E.g; C$, ICT101$, CE$, etc.
Both constant names and variable names are user defined and must not start with a special symbol or
numeric values. QBASIC doesn’t allow keywords to be used as variable or constant names. Variables and
Constant names utilize a character set of A-Z and a-z alphabetic values,0-9 numeric values and a special dot
(.) symbol might be used.
ASSIGNMENT STATEMENTS
In programming, assignment is the term for setting a variable equal to some value. Assignment allocates
data values to the memory locations represented by the variables on the left-hand side of the said
statement. These statements assign an expression to a quantity. The general form is:
Let <variable> = <value/expression>
Where expression may be a variable, a numeric data and or an arithmetic expression, or combination of
each.
An assignment statement uses the equals sign (=), but this symbol does not have the same meaning as it has
in mathematics. In QBasic, the statement
x=x+1
means that the value x is to be incremented by 1 and the result stored into the memory location reserved
for the quantity represented by x. Examples of assignment statements include;
In QBASIC, assignment statements can also be written without including the LET
OPERATORS
There are different types of operators in FORTRAN 95, the common ones include: arithmetic, relational and
logical.
10
ARITHMETIC OPERATORS: They include;
RELATIONAL OPERATORS: Relational operators are operators that are placed between expressions and that
compare the results of the expressions. They are a set of binary relational operators that take any two
numeric or character variables of the same type and kind and return a logical result. These operators are
used in conditionals to construct tests. The relational operators in QBASIC are:
operator meaning example = Equal to IF a = 15 THEN… <> Not equal to IF a <> 15 THEN… < Less than IF a <
15 THEN… <= Less or equal to IF a <= 15 THEN > greater than IF a > 15 THEN… >= greater or equal to IF a >=
15 THEN…
LOGICAL OPERATORS The type logical can have only two different values: true and false. These are variables
that can only take Boolean values. Logical variables can be operated upon by logical operators. These are:
OPERATOR MEANING
OR Logical OR
AND Logical AND
NOT Complement/Negation
INPUT/OUTPUT STATEMENTS
Data can be transferred into the program using input statement and the results be communicated to the
user or the outside world using the output statement. In programming, these statements are generally
known as input/output statements or simply i/o statements.
INPUT: This statement allows the user to enter a value for the variable while running the program. A
question mark (?) appears on the output screen waiting for the user to enter a relevant data and then press
enter key. Once the Return key or Enter key is pressed the data is stored in the variable.
Examples include;
11
Input A…… Enter Numeric constant
Input “Enter name:” ;N$…. Giving relevant message to avoid erroneous data input
OUTPUT STATEMENTS: This statement displays the results or the required output of a QBASIC program on
the screen for the user to visualize. It is implemented using the PRINT statement with the syntax as shown
below;
Examples include;
12
THE DELETE STATEMENT
DELETE: To delete a line number in a program. e.g. Delete 10 will delete line number 10 Delete 30-50 will
delete all line numbers between 30 to 50.
A label is a line number. Generally, statements in QBASIC are assigned serial number for referencing. Always
remember to include a label in a GOTO statement. You can have more than one label, but in that case they
should be different.
DATA 2
Here are four ways of assigning X the value 2 and Y the value 3:
DATA 2, 3 DATA 3
When Basic comes to a READ statement containing a list of variables, it looks for a DATA statement
containing a list of values. It reads the data values one at a time, in the order listed and beginning with the
first DATA statement in the program. A DATA statement may appear anywhere in the program - Basic will
find it. Some programmers place all DATA statements at the beginning of the program, some at the end,
while others like to place a DATA statement right after the READ statement referring to it. All READ and
DATA statements may refer to strings as well as numerical variables. Items in READ and DATA statements are
separated by commas.
Example :
In a DATA statement it is optional whether or not you put quotation marks around a string; however, if the
string contains punctuation marks then you might need quotation marks to avoid ambiguity. If one typed
just Honolulu, Hawaii in the above DATA statement, then Basic would interpret Honolulu and Hawaii as two
different strings.
If a DATA statement is too long for the screen, divide it into two statements, as below:
DATA Ann Ames, Betsy Bates, Cindy Crim, Diana Dees, Eve Evans
DATA Freda Finley, Greta Garbo, Hilary Hill, Inez Ingall, Jan Joplin
Be certain that data types in your DATA statements match variable types in your READ statements. The
sequence
13
READ A$, B
produces a "Syntax error" message, because "B" is a numeric variable but "buggy" is a string.
This is a logical statement that is vital for altering the sequential flow of a program. The syntax for IF…THEN
statement is given below;
Stmt(s)
ENDIF
If the logical expression or argument is true, the interpreter executes the statement(s) immediately after the
THEN keyword. If the argument is not TRUE, then the QBasic bypasses this line(s) and goes to the next.
If the argument is not true (if a is not equal to 15), QBasic bypasses this line and goes to the next. In some cases,
you can use the ELSE command, which tells QBasic exactly what to do if the argument is not true.
Or
Stmt(s)
ELSE
Stmt(s)
ENDIF
The ELSE command tells QBasic exactly what to do if the logical expression is not true.
You can make QBasic to execute more than one command if the argument is true. To do this, put those
commands after IF…THEN and divide them with : symbol.
This example means that if a equals to 15, QBasic will first print OK and then will go to the line labelled 5
LOOPING STATEMENTS
14
When a set of instructions are repeatedly executed a fixed number of times it is termed as Loop. A loop simply
means iteration or a repetition.
To make interesting and efficient programs, you can make QBasic to execute a part of a program more than
once. This is called looping, when QBasic goes through a part of a program over and over again. This can be done
with the GOTO command, but there are some good and more efficient ways to loop the program rather using
GOTO statements. One of them is FOR...NEXT command. Loops can be implemented using counters to keep
track of the number of repetitions to done. When using Counters, we have to follow the following points:
Example of a counter includes; suppose the variable I is initialize to zero, a counter will be implemented as
follows:
i=i+1
The term Counter is used to describe a variable that we use to count up;
FOR…NEXT LOOP
This command allows you to execute a part of a program a certain number of times. It has the syntax as shown
below:
Program statements…
NEXT [var]
Where
NB: The [step] size at the loop header is optional. If STEP is not given then BASIC assumes the increment to be 1.
Also, var increments or decrements depending on whether the number specified in STEP is positive or negative.
Example include;
FOR a = 1 TO 5
NEXT a
15
This is loop number 3
The STEP command can be implored as shown below. This will tell QBasic how to count from one number to
another:
FOR j = 0 TO 12 STEP 2
statements….
NEXT j
Examples include:
10 Let N = 15
20 FOR M = 10 TO N
30 Print M;
40 Next M
Output as displayed on
screen:
10 11 12 13 14 15
10 FOR A = 50 TO 40 STEP –1
20 Print A;
30 Next A
20 for I= 1 to 10
40 next
Output :
10 x 1 = 10
16
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
…….
WHILE...WEND
The WHILE...WEND commands continue a loop until a specified expression is false.
To use WHILE...WEND:
Stmt-1
Stmt-2
Stmt-n
WEND
Example include;
5 x = 10
10 WHILE x < 15
15 PRINT x
20 x = x + 1
25 WEND
Output :
10
11
12
13
14
17
THE DO… LOOP
This loop uses both WHILE
and UNTIL keywords.
2. Loop at least one time regardless of whether the expression is true or not.
To use DO...LOOP:
1. Specify whether the loop continues "while" the expression is true or"until" the expression is true,
using the WHILE and UNTIL statements, respectively.
Stmt-1
Stmt-2
Stmt-n
LOOP
x = 10
DO WHILE x < 15
PRINT x
x=x+1
LOOP
Stmt-1
Stmt-2
Stmt-n
LOOP
x = 10
DO UNTIL x = 15
PRINT x
18
x=x+1
LOOP
10
11
12
13
14
If you place the expression at the end of the loop instead, the program goes through the loop at least once.
x = 32
DO
PRINT x
x=x+1
This is the output because the loop was only gone through one time: 32
CLS
Sum = 0
LOOP
END
Program Output
Notice that Sum is set to zero immediately before the loop is entered. Technically, this statement is not needed.
QBasic automatically sets the values of numeric variables to zero and string variables to the null (empty) string
before execution begins. However, it is good programming practice to initialize variables yourself, rather than
depending on "default" initializations.
19
Nested loops:
You can put a loop inside of another loop in a structure called nested loops. This is done by placing one
For...Next loop within another. Each loop MUST have a unique variable name as its loop counter.
Nested loop example:
For I = 1 To 10
For J = 1 To 10
For K = 1 To 10
Statement(s)
Next K
Next J
Next I
NB: Remember that nested loops must be bracketed in LIFO order. That means that the inner-most loop ends
first and the outermost loop ends last.
EXIT DO
END IF
PRINT Voter$
Count = Count + 1
LOOP
END
If the EXIT DO statement is executed, control transfers to the first statement after LOOP However, this
statement is executed only if an invalid value for Age is encountered. The EXIT statement can he used with other
structures, such as SUB procedures. The statement EXIT SUB allows control to be transferred immediately back
to the calling program. However, a word of warning: The EXIT statement can lead to program errors. It also
makes program logic more difficult to follow. Therefore, it is best to avoid its use unless absolutely necessary.
20
Counters vs Accumulators
The term Accumulator is used to describe a variable that is being used to total up a bunch of numbers.To use a
variable as an accumulator, you must follow these two steps:
1. Set the variable that will be used as an accumulator to a starting value (usually you will start theaccumulator
at '0' [zero] )
NOTE: Accumulators are often found in loops because they repeat the process of "accumulating" numbers.
In the QBASIC code shown below, the variable named "TOTAL" is used as an accumulator.
CLS
FOR I = 1 to 5
NEXT I
AVG = TOTAL / 5
END
ARRAYS
An array is a list of variables of the same type. Arrays are useful for organizing multiple variables. To create an
array, use the DIM (dimension) command. The general syntax for declaring array is: DIM varname (arraysize),
Where varname is the array name; arraysize is the size of the array
a=2
b=4
c=6
d=8
e = 10
PRINT a, b, c, d, e
Output:
2 4 6 8 10
DIM vars(5)
vars(2) = 4
vars(3) = 6
vars(4) = 8
vars(5) = 10
Output:
2 4 6 8 10
DIM vars(5)
FOR x = 1 to 5
vars(x) = x * 2
NEXT
FOR x = 1 to 5
PRINT vars(x),
NEXT
Output:
2 4 6 8 10
1. Write a program in QBASIC to greet your friend on the occasion of New Year by using PRINT statement. The
output of the program is shown as:
Season's Greeting
Best Wishes for
A Happy and Prosperous New Year
From:
Name
Ans:
CLS
LET A$= “Season's Greeting”
22
PRINT “A Happy and Prosperous New Year:";C$
PRINT “From:";D$
PRINT “Name:";E$
23
2. There are 28 boys and 22 girls in your class. Write a program in QBASIC to fine the percentage of boys and
girls in the class.
Ans:
CLS
LET B=28
LET G=22
LET T=50
LET P1=B/T*100
LET P2=G/T*100
PRINT P1, P2
END
3. The base and height of a triangle are 15 cm and 10 cm respectively. Write a program in QBASIC to calculate
the area of the triangle.
Ans:
CLS
LET B=15cm
LET H=10cm
LET A= ^*B*H
PRINT "Area="; A
END
4. A Salesman sold 14 chairs at the rate of 275/- each and 3 tables at the rate of 650/-. Write a program in
QBASIC to calculate the total sale.
Ans:
CLS
LET C= 14 *275
LET D=3*650
LET T=C+D
PRINT “Chair, Table =”; C, D
Print "Total sale =”; T
24
5. Write a Program in QBASIC to calculate the area and circumference of a circle of the radius 14 cm.
Ans:
CLS
LET r= 14 CM
LET A= 22/7 *r*r
LET C= 2*22/7*r
PRINT "Area:"; A
PRINT "Circumference:"; C
END
Home Assignment
1. Write a program in QBASIC to find the value of the given expression when a=10, b=5, c=2.
a. aA2 +bA3+cA4
CLS
LET a=10
LET b=5
LET c= 2
LET D=a*a +b*b*b+c*c*c*c
PRINT D
END
b. ( a + b) A2
CLS
LET a=10
LET b=5
LET c= 2
LET D= (a + b) A2
PRINT D
END
c. aA2 + bA2
CLS
LET a=10
LET b=5
LET c= 2
25
LET D= aA2 + bA2
PRINT D
END
d. ( a -b +c )A2
CLS
LET a=10
LET b=5
LET c= 2
LET D= ( a -b +c ”2
PRINT D
END
2. In a class of 60 students, 90% are present. Write a program in QBASIC to calculate the number of students
present and absent.
Ans:
CLS
LET S= 60
LET P=90/100*60
LET A=60-P
PRINT “Present =”; P
PRINT "Absent =”; A
END
3. The rate of petrol is 47.60/- per litre. You paid 250/- to the pump attendant and asked for 5litres of petrol to be
filled in the tank. Write a Program in QBASIC to calculate the cost of petrol and the amount of money refunded.
Ans:
CLS
LET C=47.60*5
LET R= 250-C
PRINT "Cost of petrol=”; C
PRINT "Refund money =”; R
26
4. In a book fair, you purchased the following books:
Harry Potter Books : 950/-
English Dictionary : 1200/-
Encyclopedia : 1600/-
You gave an amount of 4000/- to the shopkeeper. Write a program in QBASIC to calculate the total amount
to
Be paid to the shopkeeper and the money refunded by him.
Ans:
CLS
LET H=950
LET E=1200
LET C=1600
LET T= H+E+C
LET R= 4000-T
PRINT “Harry Potter Books:”; H
PRINT "English Dictionary:”; E
PRINT "Encyclopedia:” ; C
PRINT "Total Cost:”; T
PRINT "Refund Money:”; R
END
5. 'LEXPO' announces 25% discount on all leather items. You purchased a leather bag that costs 660/- and a
belt priced 160/-. Write a program in QBASIC to calculate the amount to be paid to the shopkeeper.
Ans:
CLS
LET B=660
LET C=160
LET T=B+C
LET D= T*25/100
LET A=T-D
PRINT "Leather Bag:”; B
PRINT "Belt:”; C
PRINT "Total:”; T
PRINT "Discount:”; D
PRINT "Amount:” ; A
27
INPUT STATEMENT
EXERCISES
I. State whether the following statements are True/False:
28
INPUT<Numeric variable>
Example: INPUT N
1. Write a program in QBASIC to make a name striker which includes your name, class, roll no., subject and
school using INPUT statement. Display the output in the given format:
Ans:
CLS
PRINT "Name:”; N$
PRINT "Subject:”; S$
29
PRINT "School:”; SCH$
30
2. Write a program in QB ASIC format to accept the length of a rod in millimetres and express the same in centimetres
and metres.
Ans:
CLS
INPUT "Enter length of the rod in millimetres"; M
C=M/10
S= M/1000
PRINT "Length of the rod in centimetre="; C
PRINT "Length of the rod in meter ="; S
END
3. Write a program in QB ASIC format to print the total amount paid to the shopkeeper for purchasing the following
items:
Number of exercise books =10
Number of brown paper rolls=4
Ans:
CLS
INPUT "Enter the number of exercise book"; E
INPUT "Enter the number of brown paper rolls"; R
T=E+R
PRINT "Total=";T
END
4. Write a program in QB ASIC to find the value of the given expressions after taking a suitable v a lue of a and b from
the console:
i. aA2+bA2
Ans:
CLS
INPUT "Enter the value of a and b"; a, b
A=aA2+bA2
PRINT "The valueof A="; A
END
31
OUTPUT: Enter a=2, b=2
A= 2A2 +2A2
A=8
ii. a+b
ab
CLS
INPUT “Enter the value of a and b”; a, b
A= a+b
B=ab
C=A/B
PRINT "Value of A =”; A
PRINT "Value of B=”; B
PRINT "Value of C=”; C
END
5. You want to calculate the total runs made by Sachin Tendulkar only in terms of centuries and halfcenturies in test
matches. Write a program in the QBASIC format taking the numbers of centuries and half-centuries as input.
Display the total runs made by Sachin in Centuries and half-centuries separately.
Ans:
CLS
INPUT "Enter the numbers of Centuries”; C
INPUT "Enter the number of Half-Centuries”; H
T1=C*100
T2=H*50
PRINT "Total runs made by Sachin Tendulkar in centuries=”; T1
PRINT "Total runs made by Sachin Tendulkar in Half-centuries=”; T2
In an examination, 20% students failed in English and 30% failed in maths. Write a program in
32
QB ASIC format to find the total number of students who failed in both the subjects taking the total number of
students in the class as input.
Ans:
CLS
INPUTN
E= (20/100)*N
M= (30/100)*N
T=E+M
PRINT "Total=";T
END
6. Write a program in QB ASIC format to find the value of's' in the given equations, taking the suitable value of '๙, 'v'
and 'a' as input.
S= vA2-uA2
2a
Ans:
CLS
INPUT "Enter the value of"; u, v, a
S= v*v-u*u
D= 2*a
E= S/D
PRINT "The value of:" S
END
7. You purchased a Computer textbook and an Oxford dictionary. The shopkeeper allows 10% discount on the
textbook and 20% discount on the dictionary. Write a program in QB ASIC format to calculate the total discount and
the amount to be paid to the shopkeeper, taking price of the books as input.
Ans:
CLS
INPUT "Enter the price of the Computer"; Pl INPUT "Enter the price of the dictionary"; P2 Dl=10/100*Pl
33
D2=20/100*P2
T=D1+D2
A1=P1-D1
A2=P2-D2
A=A1+A2
PRINT “The Total discount:"; T
PRINT “The amount=" ; A
END
34