Python Arithmetic, Math Class & Modulo
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().
"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.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
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;.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate circle area
Declare double radius = 5.0;
Calculate double area = Math.PI * radius * radius;
Round to 2 decimal places with Math.Round(area, 2) and print "Area: 78.54".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.