Lesson 4 of 34 · Fundamentals
C# Operators: Arithmetic, Comparison and Logic
Programs crunch numbers and make decisions, and both start with operators. C# has the usual arithmetic symbols, but a couple of them behave differently from a pocket calculator — and those differences are behind some of the most common beginner bugs.
Arithmetic operators
int a = 17, b = 5;
Console.WriteLine(a + b); // 22
Console.WriteLine(a - b); // 12
Console.WriteLine(a * b); // 85
Console.WriteLine(a / b); // 3 ← integer division!
Console.WriteLine(a % b); // 2 ← remainder
When both operands are integers, / performs integer division and throws the fraction away. As soon as one side is a double, you get a real division:
Console.WriteLine(17 / 5.0); // 3.4
Console.WriteLine((double)a / b); // 3.4 — cast before dividing
Console.WriteLine((double)(a / b)); // 3 — too late, the division already happened
The remainder operator % is perfect for "every n-th" checks: n % 2 == 0 means n is even. Watch out with negative numbers — the remainder keeps the sign of the left side, so -7 % 2 is -1.
Precedence and compound assignment
*, / and % are evaluated before + and -. Use parentheses whenever it makes the intent clearer:
int score = 2 + 3 * 4; // 14
int other = (2 + 3) * 4; // 20
Updating a variable has handy shortcuts:
int gold = 100;
gold += 50; // gold = gold + 50 → 150
gold -= 20; // 130
gold *= 2; // 260
gold++; // 261
gold--; // 260
The Math class
Math holds ready-made functions:
Math.Max(3, 9) // 9
Math.Min(3, 9) // 3
Math.Abs(-7) // 7
Math.Sqrt(16) // 4
Math.Pow(2, 10) // 1024
Math.Round(2.567, 2) // 2.57
Math.Floor(2.9) // 2
Math.Ceiling(2.1) // 3
Note that Math.Round(2.5) is 2 and Math.Round(3.5) is 4: by default .NET rounds halves to the nearest even number ("banker's rounding").
Comparison and logical operators
Comparisons produce a bool: == (equal), != (not equal), <, >, <=, >=. Combine them with the logical operators:
| Operator | Meaning | Example |
|---|---|---|
&& |
and — both must be true | level >= 5 && hasSword |
|| |
or — at least one is true | isAdmin || isOwner |
! |
not | !isLocked |
int level = 12;
bool hasKey = false;
bool canOpen = level >= 10 || hasKey; // true
bool safe = !(level > 50) && level != 13; // true
&& and || short-circuit: if the left side already decides the answer, the right side is never evaluated.
Common mistakes
- Integer division:
int percent = 3 / 4 * 100;is0. Write3 * 100 / 4or usedouble. =instead of==:if (x = 5)does not compile in C# — the condition must be abool.- Odd checks with
== 1:n % 2 == 1is false for negative odd numbers. Usen % 2 != 0.
Assignment
Complete the methods in Program:
TotalSeconds(hours, minutes, seconds)converts a duration to seconds:TotalSeconds(1, 2, 3)is3723.Average(a, b, c)returns the average of three integers as adouble:Average(1, 2, 2)is1.666….IsOdd(n)returnstruefor odd numbers, including negative ones like-3.CanEnterDungeon(level, hasKey, isCursed)— a hero may enter only if they are not cursed and either their level is at least10or they carry a key.
Numbers and math operators in other languages
- JavaScript Numbers and Math Operators · JavaScript
- Python Numbers and Arithmetic Operators · Python
- Go Constants and Arithmetic Operators · Go
- Java Operators: Integer vs Double Math · Java
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .