C# Modulo Operator: Get Remainder From DivisionUse the modulo division operator to get remainders from dividing numbers.
Modulo. This operator gets a remainder. It provides a way to execute code once every several iterations of a loop. It uses the percentage sign character in the lexical syntax.
Modulo has some unique properties. As with all low-level operations, it has a specific cost. We analyze modulo division in the C# language.DivideOdd, EvenEvery Nth Element
Modulo division is expressed with the percentage sign. It is implemented with the rem instruction in the intermediate language. Rem takes the top 2 values on the evaluation stack.
Then: Rem performs the computation that returns the remainder of the division. It pushes that value onto the evaluation stack.
Here: This example demonstrates the math behind modulo. The expressions here are turned into constants during the C# compilation step.
And: No rem instructions are generated. Programs are evaluated in many phases, but the end result should always make sense.
C# program that uses modulo operator
static void Main()
// When 1000 is divided by 90, the remainder is 10.
Console.WriteLine(1000 % 90);
// When 100 is divided by 90, the remainder is also 10.
Console.WriteLine(100 % 90);
// When 81 is divided by 80, the remainder is 1.
Console.WriteLine(81 % 80);
// When 1 is divided by 1, the remainder is zero.
Console.WriteLine(1 % 1);
Notes, above program.
The program shows the remainders of the divisions of the 2 integers at each step. The runtime never performs modulo divisions here. The C# compiler does.
Important: We see that 1000 and 100 divide into parts of 90 with a remainder of 10.
Note: If the first argument to the predefined modulo operator is 81 and the second operand is 80, the expression evaluates to a value of 1.
If you use modulo on the same 2 operands, you receive 0 because there is no remainder. If you use modulo by 0, you will get a compile error or a runtime exception.DivideByZeroExceptionCompile-Time Error
You can apply modulo in a loop to achieve an interval or step effect. If you use a modulo operation on the loop index variable, you can execute code at an interval.
Note: This example shows how to write to the screen every ten iterations in the for-loop.
C# program that uses modulo division in loop
static void Main()
// Prints every tenth number from 0 to 200.
// Includes the first iteration.
for (int i = 0; i < 200; i++)
if ((i % 10) == 0)
Notes, if. Often, modulo divisions are performed in ifs and used in control flow. The three numbers in the condition in the if-statement can have any values, but we cannot divide by 0.
Modulo has several common uses in programs. You can use modulo division in loops to only execute code every several iterations. This can improve real code.
Note: We do not often need to compute numeric remainders for user consumption. The regular division operator may be more useful here.
Odd: You can use modulo to test for odd numbers and even numbers. You can define odd numbers as not-even numbers.
Modulo is slower than other arithmetic operators such as increment and decrement or even multiply. This is a hardware limitation on computers.
But: The total time required for individual modulo operations is tiny compared to other tasks such as disk reads or network accesses.
So: If you can reduce those operations with modulo division, you can improve overall performance.
The time required for modulo division depends on hardware and other factors. Some analysis from Microsoft is helpful. This page provides a table listing times required.Writing Faster Managed Code: Microsoft Docs
Estimated costs of instructions:
Add: 1 ns
Subtract: 1 ns
Multiply: 2.7 ns
Divide: 35.9 ns
You may rarely have a modulo division in a hot path and this can cause a loss of performance. This will almost always occur in a loop body or in a recursive method.
Tip: You can apply a technique called "strength reduction" manually to convert the modulo operation into a subtraction or addition.
And: To do this, add another field or local variable. Then, in each iteration of the loop, decrement it and test it against zero.
Then: When zero is reached, set it to its maximum value again. This resets the pattern.
A summary. The modulo operator is often useful. This is implemented in the CLI as a rem instruction. The C# compiler calculates modulo divisions of constants at compile-time.
Modulo division returns the remainder of the 2 operands. We use the "percent" symbol for modulo in the C# language. This is a powerful operator, but it has its nuances.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.