# C#: modulus Author: Ronald S. Holland at Total Application Works ronholland@sumtotalz.com [
 HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

• Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
<html>
<title>Example of a link </title>
<body>
<a href="http://sumtotalz.com/TotalAppsWorks/CSharp/CS_Modulus.html">
Programming Tutorials: modulus </a>
</table>
</body>
</html>

## Introduction

This tutorial assumes that you have some prior programming experience. It is not intended to be a comprehensive discussion on c# or programming. In this tutorial, we will briefly discuss variables, as we ease into c#. c# as a programming language is widespread and can be found on nearly all computers.

### C# modulus operator

What is the modulus operator? The modulo operation finds the remainder of division of one number by another number e.g., 6/5 = 1. Consider two positive numbers, where a is the dividend and n is the divisor e.g., a/n, a modulo n (abbreviated as a mod n = b), where b is the remainder. For example, the expression "9 mod 8" would evaluate to 1 because 9 divided by 8 results in a remainder of 1, whereas, "4 mod 2" results in a remainder of 0. Consider the examples in Figure 1 below.

Figure 1: Examples using the modulus function ``` 5 % 2 = 1 99 % 3 = 0 145 % 3 = 1 9 % 7 = 2 12 % 7 = 5 33 % 13 = 7 45 % 9 = 0 25 % 7 = 4 17 % 13 = 4 ```

In Figure 1, we show some examples using the modulus function. There are several simple problems that divide one number by another: what's left over when we complete the division is the remainder. The answer is easy to compute: divide 11 by 4 and take the remainder: 3. How can we compute this in a programming language like C or C#? The c# language provides a built-in function, the modulus operator ('%'), that provides a remainder that results from performing integer division. Some of the uses for the modulus function are discussed in the next section.

### The modulus operator is useful in a variety of circumstances

How is the modulus operator is useful? It is commonly used to take a randomly generated number and reduce that number to a random number on a smaller range. For example, the following Figure 3 shows the remainder when a number is devided by another number and whether the numbers from 0 to 9 are odd or even.

Output ``` using System; class CS_Modulus { public static int Main( string[] args ) { // When 1020 is divided by 91, the remainder is 19. // Console.WriteLine(1020 % 91); // // When 100 is divided by 91, the remainder is also 9. // Console.WriteLine(100 % 91); // // When 85 is divided by 80, the remainder is 5. // Console.WriteLine(85 % 80); // // When 5 is divided by 5, the remainder is zero. // Console.WriteLine(5 % 5); for (int i = 0; i < 10; i++) { if (i % 2 == 0) Console.WriteLine("{0} is even.", i); else Console.WriteLine("{0} is odd.", i); } return 0 ; } // end Main } // end class Modulus******************/ ``` ``` 19 9 5 0 0 is even. 1 is odd. 2 is even. 3 is odd. 4 is even. 5 is odd. 6 is even. 7 is odd. 8 is even. 9 is odd. ```

In Figure 2, we show in a random roll of a die, the frequency of the numbers rolled. In the next example, we take two numbers entered by the user and then compute the remainder.

Output ``` #include using namespace std; int main() { /** **************************************** * *******************************************/ using System; class CS_Modulus { public static int Main( string[] args ) { int num, num2; do { Console.WriteLine( "Enter an integer dividend" ) ; num = Convert.ToInt32( Console.ReadLine() ) ; Console.WriteLine("Enter an integer divisor" ); num2 = Convert.ToInt32( Console.ReadLine()); Console.WriteLine( num + " % " + num2 + " equals " + (num % num2) ) ; Console.WriteLine( "Enter an -1 to quit" ) ; num = Convert.ToInt32( Console.ReadLine() ) ; if (num == -1) break; } while (num != -1); return 0; } } // end class Modulus******************/ } ``` ``` Enter an integer dividend 55 Enter an integer divisor 7 55 % 7 equals 6 Enter an -1 to quit ```

### Conclusions

The purpose of this tutorial was to:

• Introduce you to the modulus function by
• Showing some examples using modulus operator
• Showing how the modulus operator is useful in a variety of circumstances
• Giving you fundamentals of the modulus operator so that you can confidently use this operator on your own. HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#