REM Function

The REM function divides one expression by another and returns the remainder. Note that the REM function is not the same as the REM statement, which is used for documentation of a program.

Format

REM(expr1, expr2)

Description

The REM function returns the value of the remainder after division is performed on expr1 by expr2. This is also called expr1 modulo expr2.

The REM function calculates according to this formula:

REM (x, y) = x - (INT (x / y) * y)

The expressions can evaluate to any numeric value, with the exception that expr2 cannot be zero.

The REM function is functionally equivalent to the MOD function.

Example

To place the remainder in the variable NUMB when 17 is divided by 5, the code would read:

NUMB = REM(17,5)

In this instance, NUM would contain 2.

In the next application the REM 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 = REM(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 this way (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

Statement and Function Reference