Many programming projects that require manipulation of numbers wind up requiring the remainder of an integer division operator. The modulus operator fills this requirement in a programming expression. You learned about the modulus operator way back in grade school when you were first learning arithmetic and division. A problem such as “What is 7 divided by 2?” would at that time have resulted in an answer of “3 with 1 left over” or “3 with remainder of 1″. It is the part that is “left over”, the remainder that the modulus operator returns.
The modulus operator is written differently in different programming languages, although most often the ampersand sign is used. Here are some examples:
Language | Modulus Operator | Example expression |
---|---|---|
“C” or C++ | % | remainder = quotient % divisor ; |
SQL | % | SELECT quotient % divisor FROM … Some DBMS such as Oracle implement it as a function as in: SELECT MOD(quotient, divisor) FROM … |
Pascal | MOD | remainder := quotient MOD divisor ; |
Perl | % | $remainder = $quotient % $divisor ; |
Java | % | remainder = quotient % divisor ; |