Modulo Examples
This page was last reviewed on Apr 27, 2023.
Dot Net Perls
Modulo. This C# operator gets a remainder. It provides a way to execute code once every several iterations of a loop. To use modulo, we specify "%"—the percentage sign character.
As with all low-level operations, modulo has a specific cost. The cost of these operations is relevant for some high-performance C# programs.
Odd, Even
An example. Modulo division is expressed with the percentage sign. It is implemented with "rem" 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 shows the math behind modulo. The expressions here are turned into constants during the C# compilation step.
Detail When 5 is divided by 3, we have 2 left over—only one 3 can be part of 5. The modulo result is 2.
using System; // When 5 is divided by 3, the remainder is 2. Console.WriteLine(5 % 3); // 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);
2 10 10 1 0
Loop example. We can use modulo in a loop for an interval or step effect. If we use a modulo operation on the loop index variable, we can execute code at an interval.
Note This example shows how to write to the screen every 10 iterations in the for-loop. We use an if-statement with modulo.
Important The 3 numbers in the condition in the if-statement can have any values, but we cannot divide by 0.
using System; class Program { 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) { Console.WriteLine(i); } } } }
0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190
Error, divide by zero. If you use modulo by 0, you will get a compile error or a runtime exception. The denominator must never be zero.
class Program { static void Main() { int zero = int.Parse("0"); int result = 100 % zero; } }
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero. at Program.Main()...
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.
Detail Some analysis from Microsoft is helpful. This page provides a table listing times required.
Add: 1 ns Subtract: 1 ns Multiply: 2.7 ns Divide: 35.9 ns
Modulo has many common uses in programs. You can use modulo division in loops to only execute code every several iterations. This can improve real code.
Detail We can test if a number is odd or even with modulo. It is important to account for a modulo of -1 on negative numbers.
Detail Suppose we want to get every third element in a List. We can use modulo for this.
List Every Nth
Modulo versus remainder. Technically the "%" operator in C# is a remainder operator. In testing, this is the same as modulo except when we are using a negative number as the divisor.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 27, 2023 (simplify).
© 2007-2024 Sam Allen.