Labs ICT
Pro Login

Arithmetic Operators

Arithmetic operators let you do math in C#. You already know most of them from school — plus, minus, multiply, divide. C# adds a few more that make common operations easier.

The Basic Operators


int a = 10;
int b = 3;

int sum = a + b;      // 13  (addition)
int diff = a - b;     // 7   (subtraction)
int product = a * b;  // 30  (multiplication)
int quotient = a / b; // 3   (division, integer — drops decimal!)
int remainder = a % b; // 1   (modulus — remainder after division)

Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Difference: {diff}");
Console.WriteLine($"Product: {product}");
Console.WriteLine($"Quotient: {quotient}");
Console.WriteLine($"Remainder: {remainder}");
    

Notice the division result: 10 / 3 gives 3, not 3.33. When you divide two integers, C# does integer division — it truncates the decimal part. Use doubles if you need the full result:


double x = 10.0;
double y = 3.0;
Console.WriteLine(x / y);  // Prints 3.33333333333333
    

The Modulus Operator (%)

Modulus gives you the remainder of a division. It's surprisingly useful:


Console.WriteLine(17 % 5);  // 2 (because 5*3=15, remainder 2)
Console.WriteLine(20 % 4);  // 0 (because 4*5=20, no remainder)
Console.WriteLine(7 % 2);   // 1 (odd number check — always 1 for odds)
    

A common trick: number % 2 == 0 means the number is even. This comes up all the time.

Increment and Decrement

Adding or subtracting 1 is so common that C# has special operators for it:


int counter = 5;
counter++;            // Now counter is 6
counter--;            // Now counter is 5 again
    

There's a subtle difference between prefix and postfix notation:


int x = 5;
int y = x++;    // Postfix: y gets 5, then x becomes 6
Console.WriteLine($"x={x}, y={y}");  // x=6, y=5

int a = 5;
int b = ++a;    // Prefix: a becomes 6, then b gets 6
Console.WriteLine($"a={a}, b={b}");  // a=6, b=6
    

Prefix (++x) increments first, then returns the value. Postfix (x++) returns the value first, then increments. When used alone on a line, both behave the same.

Try it Yourself →