Quick View
QBasic for Beginners
QBasic Tutorials: QBasic for Beginners, Chapter 2 - Mathematical Calculations with QBasic.
by Author: Maxim Kakitsev
maksim@freeuk.com
Created on 26.03.01
QBasic Tutorials: QBasic for Beginners, Chapter 2 - Mathematical Calculations with QBasic.
maksim@freeuk.com
Created on 26.03.01
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:
a = 15 / 4 + 3
PRINT a
Result on the screen:
6
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:
You:
Enter the first number 22Computer:
Enter the first number 22 Enter the second numberYou:
Enter the first number 22 Enter the second number 18Computer:
Enter the first number 22 Enter the second number 18 22 + 18 = 40 22 * 18 = 396Advanced operations:
The following program explains MOD. Type this program (except my comments) into Qbasic accurately and run it to see how MOD works.
1 CLS 'this command clears the screen, so it’s empty
INPUT "Enter the first number "; a
INPUT "Enter the second number "; b
IF b = 0 THEN 'checks 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 1 'then sends you back to line 1
END IF
CLS 'clear the screen again
c = a MOD b
d = a / b
e = a - c
f = e / b
PRINT a; "MOD"; b; "="; c
IF c = 0 THEN 'this checks if the result of a MOD b = 0, because
'it means that the result of a / b is integer
PRINT "because"; a; "/"; b; "="; d; " - integer. Try again."
DO: LOOP WHILE INKEY$ = "" 'waits for you to press a key to continue
GOTO 1 'then sends you back to the line 1
END IF
PRINT "because"; a; "/"; b; "="; d; " -not integer" 'The rest of the program
PRINT "but"; a; "-"; c; "="; e 'executes if the result of
PRINT "and"; e; "/"; b; "="; f; " - integer" 'a / b is not 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!