PROGRAMMING HINTS
U. M. A. via ITV |
Tuesdays, 7:00-9:45PM |
Instructor:
Eli C. Minkoff        
E-mail:
[email protected]
PROGRAMMING HINTS IN BASIC
TESTING DIVISIBILITY OF NUMBERS:
The INT function in BASIC evaluates
the integer part of any number, ignoring the fractional part. Thus:
INT(2.0) has the value 2
INT(2.3) has the value 2
INT(2.78) has the value 2
INT(2.99) has the value 2
INT(3.00) has the value 3
Thus, you can use IF (Y=INT(Y))
to determine if Y is an integer (the expression following IF will be
true if Y is an integer and false if it is not).
Testing whether one number is divisible by another is the same as testing
whether their quotient (one divided by the other) is an integer. Therefore,
to test if A is divisible by B, you can use
IF (A/B=INT(A/B)) because the
expression following IF will be true if A is divisible by B (and A/B is
therefore an integer) and false if it is not.
TESTING FOR PRIME NUMBERS:
An integer is considered prime if it is not divisible by any
other integer (excluding itself and the number 1).
To test whether or not a given integer is prime, you only need to test:
- whether it is divisible by 2, and
- whether it is divisible by odd numbers (3, 5, 7, etc.)
smaller than itself.
You don't need to test whether it is divisible by any larger number, because
any (positive) number divided by a larger one gives a result less than one
and greater than zero.
You also don't need to test whether it is divisible by any even
numbers greater than 2, because any number divisible by 4 (or 6, or 8,
etc.) would also be divisible by 2.
GETTING INPUT FROM THE USER:
The simplest way to get input from the user of your BASIC program
is to use the INPUT instruction.
For example, INPUT X displays a
question mark and waits until the user types in a number and presses the
<ENTER> key. Any number that was typed in becomes the value of the
variable X.
A nicer way to get input is to ask a question, using a
PRINT statement just before the
INPUT statement. For example:
PRINT "WHAT NUMBER DO YOU WANT TO TEST";
INPUT X
Notice the punctuation: no question mark after "test" (because the INPUT
statement supplies it); a semicolon (;) at the end of the PRINT statement
(to suppress the carriage return); then the INPUT statement that supplies
the question mark.