Start learning free

LESSON 24 OF 347 · CALCULATIONS WITH NUMBERS

The Modulo Operator in C#: Remainders with %

Lesson Read the full explanation

TL ; DR:

  • The modulo operator (%) calculates the remainder of dividing two values:

    Console.WriteLine(10 % 2); // Output: 0
    Console.WriteLine(15 % 4); // Output: 3
    Console.WriteLine(20 % 3); // Output: 2
    

  • It can also be used with variables:

    int a = 2;
    int b = 4;
    
    Console.WriteLine(b % a); // Output: 0
    Console.WriteLine(11 % b); // Output: 3
    


Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

The modulo operator:

The modulo operator (%) calculates the remainder of dividing two values:

Console.WriteLine(10 % 2); // Output: 0
Console.WriteLine(15 % 4); // Output: 3
Console.WriteLine(20 % 3); // Output: 2

It can also be used with variables:

int a = 2;
int b = 4;

Console.WriteLine(b % a); // Output: 0
Console.WriteLine(11 % b); // Output: 3

Math.floor() function

The Math.Floor() function returns the largest integer less than or equal to a given number:

Console.WriteLine(Math.Floor(3.9)); // Output: 3
Console.WriteLine(Math.Floor(5.2)); // Output: 5
Console.WriteLine(Math.Floor(7)); // Output: 7

We can use this function if we want to compute the integral part of some division (quotient):

int a = 2;
int b = 3;

int c = Math.Floor(b / a);
Console.WriteLine(c); // Output: 1

Quotient and remainder:

In programming, we combine both these concepts to get the quotient and remainder of some divison:

// Let's divide 26 by 3:
int quotient = Math.Floor(26 / 3);
int remainder = 26 % 3;

Console.WriteLine(quotient); // Output: 8
Console.WriteLine(remainder); // Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Modulo Operator in C#, explained

    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 % 2 is 0 for even numbers and 1 for positive odd ones.
    • Divisibility. n % 5 == 0 is true when n is a multiple of 5.
    • Last digit. 1234 % 10 is 4.
    • Wrapping around. i % 3 as i counts up gives 0, 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 / price is the number of products, 4, not the money left.
    • Swapping the operands. price % amount is 15 % 70, which is 15, 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 % b is the remainder after dividing a by b.
    • 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free