Coding/Learn/csharp/operators and math
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnArithmetic, Math Class & Modulo
6 / 20 0%
Lesson 06 of 20
5 mins read

Arithmetic, Math Class & Modulo

PreviousNext Lesson
In Plain English

C# provides standard math operators (+, -, *, /, %), shortcut compound assignments (+=, -=), and the rich System.Math utility class containing methods like Math.Round(), Math.Max(), Math.Min(), and Math.Sqrt().

The Mental Model

"System.Math is like a scientific calculator sitting in your C# toolkit—you give it complicated numbers and it instantly hands you back square roots, rounded decimals, and power exponents."

Deep Dive: How It Works

Integer Division: 7 / 2 = 3 (truncates). To get 3.5, use floating point: 7.0 / 2 or 7 / 2.0.

Modulo (%): Returns division remainder (10 % 3 = 1). Used to check if numbers are even: num % 2 == 0.

Math.Round(val, decimals): Rounds to specific decimal places.

Math.Max(a, b) & Math.Min(a, b): Finds the larger/smaller of two values.

Math.Clamp(val, min, max): Clamps a value within a min/max boundary (vital for health bars in game dev!).

Syntax Blueprint

int rem = 17 % 5;          // 2 (Remainder)
double root = Math.Sqrt(64); // 8.0
int health = Math.Clamp(damage, 0, 100);

Basic operators paired with static System.Math helper methods.

PointCore Rules to Remember

Point star
Integer Division Rules: Dividing two ints drops decimals without rounding.
Point star
System.Math Built-In: Provides high-precision mathematical algorithms out of the box.
Point star
Math.Clamp in Games: Keeps player stats inside valid 0-100 ranges effortlessly.

Common Beginner Traps & How to Fix Them

Mistake:Assuming 5 / 2 gives 2.5 when assigned to a double: double res = 5 / 2;

Why it happens: 5 / 2 executes as integer division FIRST yielding 2, then becomes 2.0.

How to fix: Write double res = 5.0 / 2;.

Pro Tip: Use Math.Abs(x) to get the absolute non-negative distance between two coordinates!

Live Interactive Example

Hit Run Code to see it live
Arithmetic and System.Math Utility
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates integer math with remainder and Math.Round precision.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate circle area

  • PointDeclare double radius = 5.0;
  • PointCalculate double area = Math.PI * radius * radius;
  • PointRound to 2 decimal places with Math.Round(area, 2) and print "Area: 78.54".
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates