C#: modulus

Author: Ronald S. Holland
at
Total Application Works

ronholland@sumtotalz.com





To contact us



[
HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2013 All Rights Reserved Total Application Works


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


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.

 

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


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.

 

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******************/
 
Output
 
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.

Figure 2: Modulo operator


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.

 

#include <iostream>

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******************/
}
 
Output
 
Enter an integer dividend
55

Enter an integer divisor
7

55 % 7 equals 6
Enter an -1 to quit
 
Figure 3: Example II using the modulus function


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#


To contact us

© 2002 - 2013 All Rights Reserved Total Application Works