DotNet-100-PreWork-GitBook
  • DotNet PreWork
  • Part 0: Intro
    • 1.0: Installation
    • 2.0: Getting Started
    • 3.0: Configuration
  • Part 1: HTML Fundamentals
    • Basic Tags
    • H & P Tags
    • Lists
    • Links
    • Images
    • Tables
    • Divs
    • Inputs
    • Forms
    • Sections
    • Articles & Headers
    • Footers
    • Navs
    • iFrames
  • Part 2: CSS Fundamentals
    • CSS Set Up
    • Classes
    • Ids
    • Margins & Padding
    • Fonts
    • Backgrounds
    • Widths & Heights
    • Borders
    • Positions
  • Part 3: C# Fundamentals
    • 1.0: Hello World
    • 2.0: Variables
    • 3.0: Basic Types
    • 3.0a: Types Challenges
    • 3.0b: Types Challenges Answers
    • 4.0: Operators
    • 5.0: Strings
    • 6.0: Booleans
      • 6.0a: Boolean Challenge
      • 6.0b: Challenge Answers
    • 7.0: Conditionals
      • 7.1: Switch Case
      • 7.2: If Statements
      • 7.3: Ternary Expressions
    • 8.0: Numbers
    • 9.0: Loops
      • 9.0a: Loop Challenges
      • 9.0b: Challenge Answers
    • 10.0: Classes and Objects
    • 11.0: Properties
    • 12.0: Useful-Links
Powered by GitBook
On this page
  • File Location
  • Math Operators
  1. Part 3: C# Fundamentals

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.

Previous3.0b: Types Challenges AnswersNext5.0: Strings

Last updated 7 years ago