The MOD function divides one expression by another and returns the remainder.
Format
MOD(expr1, expr2) |
Description
The MOD function returns the value of the remainder after division is performed on expr1 by expr2.
The MOD function calculates according to this formula:
MOD (x, y) = x - (INT (x / y) * y) |
The expressions can evaluate to any numeric value, with the exception that expr2 cannot be zero.
The MOD function is functionally equivalent to the REM function.
Example
To place the remainder in the variable NUMB when 17 is divided by 5, the code would read:
NUMB = MOD(17,5) |
In this instance, NUM would contain 2.
In the next application the MOD function is used to calculate a customer’s change after a purchase.
PRINT "HOW MUCH DO YOU HAVE TO SPEND? $" : INPUT AMOUNT NUMBER = INT(AVAILABLE/PRICE) CHANGE = MOD(AVAILABLE,PRICE) PRINT "EACH ITEM COSTS " : PRICE"2,$" : "." PRINT "FOR " : AMOUNT "2,$" : " YOU CAN GET " : NUMBER : " ITEMS." PRINT "YOUR CHANGE WILL BE " : CHANGE "2,$" : "." |
A sample run might appear as follows (with the operator’s input in bold):
HOW MUCH DO YOU HAVE TO SPEND? $4 EACH ITEM COSTS $1.25. FOR $4.00 YOU CAN GET 3 ITEMS. YOUR CHANGE WILL BE $0.25. |
See Also