QBasic
was obviously created for us to have fun, play games, draw nice graphics and
even make sounds.
But,
as you might guess, nothing good comes without a bit of effort that has to be
put in it.In the most QBasic programs
a bit of math has to be done.
The
math…Doh!
If
you hate mathematics, don’t worry.Qbasic will do it all for you, you just need to know how to tell QBasic
to do that.
Qbasic
can perform the following mathematical operations:
Operator
What it does
Example
Result
+
Add
7
+ 2
9
-
Subtract
7 –
2
5
*
Multiply
7
* 2
14
/
Divide
7
/ 2
3.5
Examples:
1.a = 15 / 4 + 3
PRINT a
Result
on the screen – 6
2.PRINT “Enter the first number”
INPUT a
PRINT “Enter the second number”
INPUT b
c = a + b
d = a * b
PRINT a; “+”; b; “=”; c
PRINT a; “*”; b; “=”; d
END
When
you run this program it goes like this:
Computer:
Enter the first number
You:22
Computer:Enter the
second number
You:18
Computer:22 + 18 = 40
22
* 18 = 396
Advanced
operations:
Operator
What it does
Example
Result
\
divides and
turns the result into an integer (the whole number)
7
\ 2
3
^
Raises a
number to the power of another number
3 ^ 4
(means: 3 * 3 * 3 * 3)
2.5 ^ 3
(means:2.5 * 2.5 * 2.5)
243
15.625
SQR
Calculates
the square root of a number
SQR(9)
SQR(16)
SQR(5)
3
(because: 3 ^ 2 = 9)
4
(because: 4 ^ 2 = 16)
2.236
MOD
Divides two
numbers, and if the result is not an integer (for example - 3.25), finds out
how much to subtract from the first number in order to get the integer
result.
17 MOD 5
2
(because: 17 / 5 = 3.4
17 – 2 = 15
15 / 5 = 3)
The
following program explains MOD.Type
this program (except my comments) into Qbasic accurately and run it to see how
MOD works.
1 CLSthis command clears the
screen, so it’s empty
INPUT "Enter the first number "; a
INPUT "Enter the second number "; b
IF
b = 0 THENchecks if the second number is zero, because
you can’t divide by zero
PRINT "the second number cannot be 0.Try again."
DO: LOOP WHILE INKEY$ = ""waits for you to press a key to continue
GOTO 1then
sends you back to line 1
END IF
CLSclear the screen again
c
= a MOD b
d
= a / b
e
= a - c
f
= e / b
PRINT a; "MOD"; b; "="; c
IF c = 0 THENthis checks if the
result of a MOD b= 0,
because it means that
DO: LOOP WHILE INKEY$ = ""waits
for you to press a key to continue
GOTO 1then
sends you back to the line 1
END IF
PRINT "because"; a; "/"; b; "="; d; "
-not integer"The rest of
the program executes if the
PRINT "but"; a; "-"; c; "="; eresult of a / b is not integer
PRINT "and"; e; "/"; b; "="; f; " -
integer"
END
This
program may look very complicated for you, but don’t worry.Qbasic is a very easy language to learn and
soon you’ll be having fun with it.I
promise you!