4.0: Operators

In this module we'll discuss the various operators in C#.

File Location

  1. Right click on the solution you made in the first module, go to Add > New Project.

  2. Select Console App (.NET Framework).

  3. Name it 04_Operators.

  4. Write your code for this module within Program.cs of that project.

Math Operators

The first thing to know is that C# uses common math using the operators + - * /. Ones you should already know. For example, add the following code:

Console.WriteLine(5 + 2) //result = 7
Console.WriteLine(10 - 6) //result = 4
Console.WriteLine(2 * 3) //result = 6
Console.WriteLine(10 / 2) //result = 5

There is another common operator called the modulus operator - %. This is not a percentage, but the remainder when two numbers are divided. For example:

Console.WriteLine(10 % 2) //result = 0 (10/2 = 5 w/ remainder of 0)
Console.WriteLine(10 % 9) //result = 1 (10/9 = 1 w/ remainder of 1)
Console.WriteLine(16 % 3) //result = 1 (16/3 = 5 w/ remainder of 1)
Console.WriteLine(19 % 5) //result = 4 (19/5 = 3 w/ remainder of 4)

% can be used to easily determine if a number is even or odd, even numbers % 2 are always 0.

To learn the modulus, the best thing to do is problems in your head. What is the result of the following:

10 % 3;
9 % 2;
100 % 22;
13 % 12;

In your Program.cs check on the answers above, and then practice using the different operators by logging a few things to the console, especially the modulus operator.

Last updated