Arithmetic and Assignment Operators
Use +, -, *, /, % and compound assignment forms.
Arithmetic Operators
C# gives you the classic math operators: + add, - subtract, * multiply, / divide, and % remainder (modulo).
They work on numeric types like int, long, double, and decimal.
using System;
class Program {
static void Main() {
int a = 10, b = 3;
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);
}
}Integer Division
When both operands are integers, / performs integer division and truncates the fractional part.
10 / 3 is 3, not 3.33.
using System;
class Program {
static void Main() {
int result = 10 / 3;
Console.WriteLine(result);
Console.WriteLine(7 / 2);
}
}All lessons in this course
- Arithmetic and Assignment Operators
- Comparison and Logical Operators
- Bitwise and Shift Operators
- Operator Precedence and Associativity