What the operator does
The modulo operator % gives the remainder of a division. If you divide 15 by 4, the quotient is 3 (four fits three times into 15) and the remainder is 3 (15 minus 12). The expression 15 % 4 produces that remainder.
Console.WriteLine(10 % 2); // 0
Console.WriteLine(15 % 4); // 3
Console.WriteLine(20 % 3); // 2
It works on variables too:
int a = 2;
int b = 4;
Console.WriteLine(b % a); // 0
Console.WriteLine(11 % b); // 3
In C# the symbol is formally the remainder operator, and the two names mean the same thing for positive numbers. The difference only appears with negative values, covered below.
Quotient and remainder together
Integer division gives the quotient, and % gives the remainder. Both come from the same division:
int quotient = 26 / 3;
int remainder = 26 % 3;
Console.WriteLine(quotient); // 8
Console.WriteLine(remainder); // 2
This fits the identity 3 * 8 + 2 == 26. In C#, the quotient is a plain / between two int values, which drops the fraction. You don't need Math.Floor. Calling it on an int division, as in Math.Floor(26 / 3), is rejected by the compiler as an ambiguous call, because an int converts equally well to double and decimal.
Useful patterns
- Even or odd.
n % 2is0for even numbers and1for positive odd ones. - Divisibility.
n % 5 == 0is true whennis a multiple of 5. - Last digit.
1234 % 10is4. - Wrapping around.
i % 3asicounts up gives0, 1, 2, 0, 1, 2, ..., a handy way to cycle through three options or array positions.
Details specific to C#
Negative numbers. The result of % takes the sign of the left operand. So -7 % 3 is -1 and 7 % -3 is 1. Some languages give a non-negative result here, so don't assume x % n is always in the range 0 to n-1 when x can be negative.
Doubles. % also works on floating-point values: 5.5 % 2 is 1.5. Most uses are with integers.
Division by zero. 5 % 0 with integer operands throws a DivideByZeroException at run time, so check the divisor if it can be zero. If the zero is a literal in your source, the compiler rejects it before the program runs.
Precedence. % has the same level as * and /, so it runs before + and - and left to right among equals.
Splitting a total into units
Quotient and remainder together convert a number into larger and smaller units. Take 125 minutes. 125 / 60 is 2 hours, and 125 % 60 is 5 minutes left over, so 125 minutes is 2 hours and 5 minutes. The same two lines work for cents and dollars, or for any pair of units where one fits a whole number of times into the other.
The exercise
Andy has amount dollars (70) and each product costs price dollars (15). He buys as many products as he can afford. You print how much money he has left.
He can buy 4 products, which cost 60 dollars. The money left is what did not divide evenly by the price, which is exactly the remainder of amount divided by price: 70 - 60 = 10.
The solution in C#
int amount = 70;
int price = 15;
Console.WriteLine(amount % price);
This prints 10.
Checking the answer
Verify the result by combining both operators: price * (amount / price) + amount % price must give back amount. Here that is 15 * 4 + 10, which is 70. If you ever doubt a remainder, this identity is a quick test, and it also shows that the remainder is always smaller than the divisor.
Common mistakes
- Using
/instead of%.amount / priceis the number of products,4, not the money left. - Swapping the operands.
price % amountis15 % 70, which is15, because 70 does not fit even once into 15. - Reading % as a percentage. It has nothing to do with percentages in C#.
Key takeaways
a % bis the remainder after dividingabyb.- Together with
/on integers it gives quotient and remainder. - In C# the sign of the result follows the left operand, and a zero divisor throws for integers.