The Modulus Operator (%)

The modulus operator, represented by the percent symbol ((%), is the last unmentioned binary operator of Table 7.1. The modulus operator has the same precedence as the division and multiplication operators. It can only be used with integer numbers and returns the remainder of dividing the first (left) operand with the second (right) operand. For example 20 % 7 is 6 because 7 goes into 20 two times, 2 * 7 = 14 and 20 - 14 = 6, which is the remainder. An often-used algebraic notation to denote a modulus calculation is the abbreviation mod. Thus, 20 % 7 in mathematics is often written 20 mod 7. Table 7.2 gives an overview of the five binary operators and how typical arithmetic expression are written in C#.

Table 7.2. The ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.