C#: modulus


HOME]  Consulting  Design  Maintenance  Project  Testing  Training  Turnkey  Java  C++  SQL  HTML  JavaScript  C# 
© 2002  2013 All Rights Reserved Total Application Works
<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 
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.
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 

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 
#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 
The purpose of this tutorial was to:
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