MOD
m = a MOD b
Calculates the Modulus. Returns the remainder of a/b
as
integer. Equivalent syntax to % and MDL.
Example 1
print 5 mod 2 ' Output 1
print 12 mod 5 ' Output 2
Example 2: ASCII table
' LOCATE MOD CHR ASC.bas SmallBASIC 0.12.2 [B+=MGA] 2016-03-23
'
' LOCATE row, column : sets the next print location on screen, rows down, columns across
' a MOD b : returns the remainder of a/b as integer
' CHR : returns the CHaRracter for the ASC number
' for demo we will print a chart of CHR for ASC numbers 32-128
' ASC(Character) : is a number code for a print characters, 32 is the code for a space
' ? : is shortcut for PRINT
' RIGHT(string,n) : returns right most n characters of string
' STR(n) : returns a number in string form
' : : code statement seperator often used with LOCATE row, column : ? string
LOCATE 1,16 : PRINT "ASC Table 30-129:" ' locate print spot, print title for our app
FOR column = 0 to 9 ' print a header, 10 numbers plus + (to add to row value)
LOCATE 2, column * 5 + 4 : PRINT "+"; column
NEXT
FOR row = 3 to 12
LOCATE row, 0 : ? RIGHT(" " + STR(row * 10) + ":", 4)
NEXT
'main table
FOR ASCnumber = 30 to 129 ' note ASC(32) = space so wont see anything in Table
10 ' \ rounds division down to integer
row = ASCnumber \ MOD 10) * 5 + 5 ' times 5 to space out the characters printed plus 5 for column labels
column = (ASCnumber LOCATE row, column : ? CHR(ASCnumber)
NEXT
Example 2: Prime or lowest divisor table
' more MOD.bas SmallBASIC 0.12.2 [B+=MGA] 2016-03-23
'
' n MOD m - returns the remainder of n divided by m, if 0 then m divides n perfectly
' another way to do MOD in SmallBASIC is to use symbol %, n%m is same as n MOD m
' MOD is great to tell if a number is divisible by another (leaves no remainders)
' a number not divisible by any number less to it other than 1, is called a prime number
' here we will list first 100 numbers and tell if prime or give the lowest divisor
'table setup: title header and row labels
LOCATE 0,0 : PRINT "P=Prime or Lowest Divisor Table"
FOR column = 0 to 9 ' print a header, 10 numbers plus + (to add to row value)
LOCATE 2, column * 2 + 7 : PRINT "+"; column
NEXT
FOR row=3 to 12
LOCATE row, 0 : PRINT RIGHT(" "+STR(row * 10 - 30) + ":", 6)
NEXT
'main table data
FOR n= 1 to 99
IF n = 1 THEN
" O" ' one is one, neither prime nor not prime
report = ELSE
" P" ' letter code for Prime
report = FOR i = 2 TO n - 1
IF n % i = 0 THEN ' <== if n MOD i=0 or n%i=0, THEN i divides n perfectly
" " + STR(i)
report = EXIT ' we found lowest divisor get out of loop
END IF
NEXT
ENDIF
10 + 3 ' n\10 is our number divided by 10 and rounded down,
row = n \ ' call it the tens row offset 3 rows down for title and header and blank line
10 * 2 + 7 ' <== use MOD to LOCATE the column (*2 column width + 7 row label offset)
column = n % LOCATE row, column : ? report ' ? short for print
NEXT
"O=one is neither prime nor not prime" ' ?:?"..." print blank line first ?:?
Code samples using MOD
000 hello.bas
001 3 ways to print hello 5 times.bas
002 numeric variables.bas
006 arrays+.bas
100lines.bas
3d rotating cube with message.bas
3d wire cube v1.bas
3d wire cube.bas
3d_palmx.bas
3d_torus.bas
3dtorus.bas
agendus.bas
analog clock.bas
angel feather.bas
another look at trig functions.bas
B+B.bas
bairstow.bas
base64.bas
bb2fork smurf.bas
bezier's autograph book.bas
biorythms.bas
blackjack.bas
blushing snowflakes.bas
bonkers.bas
bopit.bas
bowling7.bas
bpf1.bas
bpf2.bas
btn 21.bas
Language
AND
AS
BAND
BG
BOR
BYREF
CALL
CASE
CATCH
CONST
DECLARE
DEF
DO
ELIF
ELSE
ELSEIF
END
END TRY
ENDIF
EQV
EXIT
FALSE
FI
FOR
FUNC
GOSUB
GOTO
IF
IFF
IMP
IN
LABEL
LET
LIKE
LOCAL
LSHIFT
MDL
MOD
NAND
NEXT
NOR
NOT
ON
OR
REM
REPEAT
RETURN
RSHIFT
SELECT
STEP
STOP
SUB
THEN
THROW
TO
TRUE
TRY
UNTIL
USE
USG
USING
WEND
WHILE
XNOR
XOR
If there is insufficient information on this page and you wish learn more about MOD,
please send an email to smallbasic@gmail.com. You can help to improve information about
MOD by submitting a pull request,
click View Source for details. Note, an offline language reference text file is also available - see the Download section.