Mathematical Calculations
Chapter 3Chapter 4Chapter 5
Chapter II
Mathematical Calculations
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:
OperatorWhat it doesExampleResult+Add7 + 29-Subtract7 - 25*Multiply7 * 214/Divide7 / 23.5
Example 1:
a = 15 / 4 + 3 PRINT a
Result on the screen:
6
Example 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:
Enter the first number 22
Computer:
Enter the first number 22 Enter the second number
You:
Enter the first number 22 Enter the second number 18
Computer:
Enter the first number 22 Enter the second number 18 22 + 18 = 40 22 * 18 = 396
Advanced operations:
OperatorWhat it doesExampleResult\divides and turns the result into
an integer(the whole number)7 \ 23^Raises a number to the
power of another number3 ^ 4
(means: 3*3*3*3)
2.5 ^ 3
(means: 2.5*2.5*2.5)
243
15.625SQRCalculates the square root
of a numberSQR(9)
SQR(16)
SQR(5)3
(because: 3^2=9)
4
(because: 4^2=16)
2.236MODDivides 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 52
(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 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!
Bạn đang đọc truyện trên: AzTruyen.Top