C# Operators & Math

Arithmetic, logical operators and Math.

✍️ 2 exercises in this lesson
  • Arithmetic: + - * / % ++ -- (int / int gives an int)
  • Comparison: == != > < >= <=
  • Logical: && || !
  • Null operators: ?? (default if null) and ?. (safe member access)

The Math class: Math.Max(), Math.Sqrt(), Math.Pow(), Math.Round(), Math.Abs().

Key Takeaways: C# Operators & Math

Mastering C# Operators & Math helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C# Operators & Math Code Example

C#
Console.WriteLine(17 / 5);
Console.WriteLine(17 / 5.0);
Console.WriteLine(17 % 5);
Console.WriteLine(Math.Sqrt(81));
Console.WriteLine(Math.Pow(2, 10));
Console.WriteLine(Math.Round(3.14159, 2));

string? nickname = null;
Console.WriteLine(nickname ?? "Guest");
Console.WriteLine(nickname?.Length ?? 0);
Output
3
3.4
2
9
1024
3.14
Guest
0
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C# Operators & Math Practice Exercises

Check what you learned in “C# Operators & Math”.

/ 2 solved
Fill in the blank

Use a default when nickname is null.

string shown = nickname  "Guest";
✓ Correct! Not quite. Try again.
Multiple choice

What is 17 / 5 when both are int?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services