Mod Operator
Returns the remainder obtained by dividing a numeric expression by another numeric expression.
Syntax
number Mod divisor
Parameters
- number
- The number being divided (the dividend).
- divisor
- The number which the dividend is divided by.
Return Values
Returns the remainder after dividing number by divisor.
Description
Mod is used to get the remainder after dividing 2 numbers. The remainder is a whole number. One clever use of the Mod instruction is to determine if a number is odd or even.
The method in which the Mod instruction works is as follows:
The number is divided by divisor. The remainder left over after this division is then the return value for the Mod instruction.
See Also
Abs, Atan, Atan2, Cos, Int, Not, Sgn, Sin, Sqr, Str$, Tan, Val
Mod Operator Example
The example shown below determines if a number (var1) is even or odd. When the number is even the result of the Mod instruction will return “0”. When the number is odd, the result of the Mod instruction will return “1”.
Function modtest
....Integer var1, result
....Print "Enter an integer number:"
....Input var1
....result = var1 Mod 2
....Print "Result = ", result
....If result = 0 Then
........Print "The number is EVEN"
....Else
........Print "The number is ODD"
....EndIf
Fend
Some other example results from the Mod instruction from the Command window.
> Print 36 Mod 6
> 0
> Print 25 Mod 10
> 5
>