10 minlesson

Arithmetic Operators

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:

OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division20 / 45
%Modulus (remainder)17 % 52
csharp
1int a = 10;
2int b = 3;
3
4Console.WriteLine($"a + b = {a + b}"); // 13
5Console.WriteLine($"a - b = {a - b}"); // 7
6Console.WriteLine($"a * b = {a * b}"); // 30
7Console.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):

csharp
1int x = 7;
2int y = 2;
3
4int result = x / y; // 3, not 3.5!
5Console.WriteLine(result);
6
7// To get decimal result, use double
8double preciseResult = (double)x / y; // 3.5
9Console.WriteLine(preciseResult);

The Modulus Operator

The modulus operator % returns the remainder of division:

csharp
1Console.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

csharp
1int number = 42;
2
3// Check if even or odd
4bool isEven = number % 2 == 0; // true
5bool isOdd = number % 2 != 0; // false
6
7// Get last digit
8int lastDigit = number % 10; // 2
9
10// Wrap around (cycling through values)
11int hour = 25;
12int normalizedHour = hour % 24; // 1

Operator Precedence

C# follows mathematical order of operations (PEMDAS):

  1. Parentheses ()
  2. Multiplication, Division, Modulus * / %
  3. Addition, Subtraction + -
csharp
1int result1 = 2 + 3 * 4; // 14 (not 20!)
2int result2 = (2 + 3) * 4; // 20
3int result3 = 10 - 4 / 2; // 8 (not 3!)
4int result4 = (10 - 4) / 2; // 3

More Precedence Examples

csharp
1// Multiplication before addition
2int a = 5 + 3 * 2; // 5 + 6 = 11
3
4// Left to right for same precedence
5int b = 20 / 4 * 2; // 5 * 2 = 10
6
7// Parentheses override precedence
8int c = 20 / (4 * 2); // 20 / 8 = 2
9
10// Complex expression
11int d = 10 + 20 * 3 - 5; // 10 + 60 - 5 = 65

Increment and Decrement

Special operators to add or subtract 1:

csharp
1int count = 5;
2
3count++; // count is now 6 (same as count = count + 1)
4count--; // count is now 5 (same as count = count - 1)
5
6// Pre vs Post increment
7int a = 5;
8int b = a++; // b = 5, then a becomes 6
9Console.WriteLine($"a = {a}, b = {b}"); // a = 6, b = 5
10
11int x = 5;
12int y = ++x; // x becomes 6, then y = 6
13Console.WriteLine($"x = {x}, y = {y}"); // x = 6, y = 6

Unary Operators

csharp
1int positive = 5;
2int negative = -positive; // -5
3
4// Negation
5int value = 10;
6int negated = -value; // -10
7
8// Positive (rarely used)
9int same = +value; // 10

Math Class

For advanced operations, use the Math class:

csharp
1// Power (exponentiation)
2double squared = Math.Pow(5, 2); // 25
3double cubed = Math.Pow(2, 3); // 8
4
5// Square root
6double root = Math.Sqrt(16); // 4
7
8// Absolute value
9int abs = Math.Abs(-10); // 10
10
11// Rounding
12double rounded = Math.Round(3.7); // 4
13double floor = Math.Floor(3.7); // 3
14double ceiling = Math.Ceiling(3.2); // 4
15
16// Min and Max
17int min = Math.Min(5, 10); // 5
18int max = Math.Max(5, 10); // 10
19
20// Trigonometry
21double sin = Math.Sin(Math.PI / 2); // 1
22
23// Constants
24double pi = Math.PI; // 3.14159...
25double e = Math.E; // 2.71828...

Practical Examples

csharp
1// Calculate area of a circle
2double radius = 5;
3double area = Math.PI * Math.Pow(radius, 2);
4Console.WriteLine($"Area: {area:F2}"); // Area: 78.54
5
6// Calculate average
7int score1 = 85;
8int score2 = 90;
9int score3 = 78;
10double average = (score1 + score2 + score3) / 3.0;
11Console.WriteLine($"Average: {average:F1}"); // Average: 84.3
12
13// Convert temperature
14double 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 Math class for advanced operations

Next, we'll explore assignment and comparison operators.