Arithmetic Operators
Arithmetic operators let you perform mathematical calculations in your programs. This lesson covers the basic math operators in C#.
Basic Arithmetic Operators
C# supports the standard mathematical operators:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 20 / 4 | 5 |
% | Modulus (remainder) | 17 % 5 | 2 |
csharp1int a = 10;2int b = 3;34Console.WriteLine($"a + b = {a + b}"); // 135Console.WriteLine($"a - b = {a - b}"); // 76Console.WriteLine($"a * b = {a * b}"); // 307Console.WriteLine($"a / b = {a / b}"); // 3 (integer division!)8Console.WriteLine($"a % b = {a % b}"); // 1
Integer Division
When dividing integers, the result is also an integer (decimals are truncated):
csharp1int x = 7;2int y = 2;34int result = x / y; // 3, not 3.5!5Console.WriteLine(result);67// To get decimal result, use double8double preciseResult = (double)x / y; // 3.59Console.WriteLine(preciseResult);
The Modulus Operator
The modulus operator % returns the remainder of division:
csharp1Console.WriteLine(10 % 3); // 1 (10 = 3*3 + 1)2Console.WriteLine(15 % 5); // 0 (15 = 5*3 + 0)3Console.WriteLine(17 % 4); // 1 (17 = 4*4 + 1)
Common Uses of Modulus
csharp1int number = 42;23// Check if even or odd4bool isEven = number % 2 == 0; // true5bool isOdd = number % 2 != 0; // false67// Get last digit8int lastDigit = number % 10; // 2910// Wrap around (cycling through values)11int hour = 25;12int normalizedHour = hour % 24; // 1
Operator Precedence
C# follows mathematical order of operations (PEMDAS):
- Parentheses
() - Multiplication, Division, Modulus
* / % - Addition, Subtraction
+ -
csharp1int result1 = 2 + 3 * 4; // 14 (not 20!)2int result2 = (2 + 3) * 4; // 203int result3 = 10 - 4 / 2; // 8 (not 3!)4int result4 = (10 - 4) / 2; // 3
More Precedence Examples
csharp1// Multiplication before addition2int a = 5 + 3 * 2; // 5 + 6 = 1134// Left to right for same precedence5int b = 20 / 4 * 2; // 5 * 2 = 1067// Parentheses override precedence8int c = 20 / (4 * 2); // 20 / 8 = 2910// Complex expression11int d = 10 + 20 * 3 - 5; // 10 + 60 - 5 = 65
Increment and Decrement
Special operators to add or subtract 1:
csharp1int count = 5;23count++; // count is now 6 (same as count = count + 1)4count--; // count is now 5 (same as count = count - 1)56// Pre vs Post increment7int a = 5;8int b = a++; // b = 5, then a becomes 69Console.WriteLine($"a = {a}, b = {b}"); // a = 6, b = 51011int x = 5;12int y = ++x; // x becomes 6, then y = 613Console.WriteLine($"x = {x}, y = {y}"); // x = 6, y = 6
Unary Operators
csharp1int positive = 5;2int negative = -positive; // -534// Negation5int value = 10;6int negated = -value; // -1078// Positive (rarely used)9int same = +value; // 10
Math Class
For advanced operations, use the Math class:
csharp1// Power (exponentiation)2double squared = Math.Pow(5, 2); // 253double cubed = Math.Pow(2, 3); // 845// Square root6double root = Math.Sqrt(16); // 478// Absolute value9int abs = Math.Abs(-10); // 101011// Rounding12double rounded = Math.Round(3.7); // 413double floor = Math.Floor(3.7); // 314double ceiling = Math.Ceiling(3.2); // 41516// Min and Max17int min = Math.Min(5, 10); // 518int max = Math.Max(5, 10); // 101920// Trigonometry21double sin = Math.Sin(Math.PI / 2); // 12223// Constants24double pi = Math.PI; // 3.14159...25double e = Math.E; // 2.71828...
Practical Examples
csharp1// Calculate area of a circle2double radius = 5;3double area = Math.PI * Math.Pow(radius, 2);4Console.WriteLine($"Area: {area:F2}"); // Area: 78.5456// Calculate average7int score1 = 85;8int score2 = 90;9int score3 = 78;10double average = (score1 + score2 + score3) / 3.0;11Console.WriteLine($"Average: {average:F1}"); // Average: 84.31213// Convert temperature14double celsius = 25;15double fahrenheit = (celsius * 9 / 5) + 32;16Console.WriteLine($"{celsius}°C = {fahrenheit}°F"); // 25°C = 77°F
Summary
In this lesson, you learned:
- Basic arithmetic operators:
+,-,*,/,% - Integer division truncates decimals
- Modulus
%returns the remainder - Operator precedence follows mathematical rules
- Increment
++and decrement--operators - The
Mathclass for advanced operations
Next, we'll explore assignment and comparison operators.