Arithmetic
What you'll learn:
- predict the result of division and remainder for whole numbers, negative ones included, and notice an
intoverflow; - change a variable with
++,--and+=and tellx++from++x; - compare values and combine conditions with
&&,||and!; - choose between
doubleanddecimaland avoid comparing fractions with==.

C# counts like a calculator, but this calculator has its own rules. The main one: it remembers the type of every number and answers with a number of the same type.
Imagine sharing 17 sweets between five children. You cannot cut the sweets, so each child gets three, and two stay with you. Dividing whole numbers in C# works the same way: the result has no fractional part, and whatever could not be shared is called the remainder.
int sweets = 17;
int kids = 5;
Console.WriteLine(sweets + kids); // 22
Console.WriteLine(sweets - kids); // 12
Console.WriteLine(sweets * kids); // 85
Console.WriteLine(sweets / kids); // 3
Console.WriteLine(sweets % kids); // 2Let's go through what the program printed:
- The first two lines put the number of sweets and the number of children into variables.
- Addition, subtraction and multiplication print
22,12and85, as on a calculator. sweets / kidsprints3: that is how many sweets each child gets. The/operator gave the quotient.sweets % kidsprints2: that is how many sweets stay with you. The%operator gave the remainder.
An ordinary calculator would show 3.4, but C# showed 3.
Integer division
The signs +, -, *, / and % are called operators, and the numbers an operator works on are called operands. When both operands of / are integers, C# performs integer division: the fractional part of the result is dropped, not rounded.
17 / 5is3, although on paper you get3.4.19 / 5is also3, although3.8is closer to four.-17 / 5is-3, not-4: a negative result loses its fraction too.
Integer division always moves the result toward zero: a positive one down, a negative one up.
Getting a fraction
You get a fractional result when at least one operand has the type double. C# then turns the other operand into a double on its own, and the fractional part survives. Here are two ways:
Console.WriteLine(7.0 / 2); // 3.5
Console.WriteLine((double)sweets / kids); // 3.4As you remember from the theme on types, a fractional number prints according to the computer's regional settings: 3.5 here, and 3,5 with Russian settings.
A fractional literal
A number with a point, such as 7.0, has the type double. The literal 2 next to it turns into a double, and you get 3.5. This way works when the number is written right in the code.
Casting a variable
Variables need another way. (double)sweets takes the value of sweets and turns it into a double, while the variable itself stays an int. The cast happens before the division, so it is 17.0 that gets divided by 5, and you get 3.4.
Remainder
The % operator gives the remainder of a division. The quotient and the remainder are always linked: a / b * b + a % b equals a. For the sweets that is 3 * 5 + 2, which is 17 again.
This is where the sweets picture ends: there are no negative sweets, but C# divides negative numbers too. The quotient moves toward zero, but a / b * b + a % b still equals a. So the remainder takes the sign of the dividend, the left operand:
Console.WriteLine(-17 / 5); // -3
Console.WriteLine(-17 % 5); // -2
Console.WriteLine(17 % -5); // 2Let's check each line:
-17 / 5is-3: the fraction is dropped, and the result moves toward zero.-17 % 5is-2. Check it:-3 * 5gives-15, and-2is exactly what is missing to reach-17.17 % -5is2: the dividend17is positive, so the remainder is positive too, although the divisor is negative.
Step through the program and watch q and r: at each check they add back up to the dividend.
1int sweets = 17;2int kids = 5;3int q = sweets / kids;4int r = sweets % kids;5Console.WriteLine(q);6Console.WriteLine(r);7Console.WriteLine(q * kids + r);8int n = -17;9q = n / kids;10r = n % kids;11Console.WriteLine(q);12Console.WriteLine(r);13Console.WriteLine(q * kids + r);
sweets is how many sweets there are: 17.
Interviewers like to ask this: what sign does the remainder have when the dividend is negative? In math the remainder is usually taken as non-negative, but in C# it takes the sign of the dividend.
Order of operations
The operators *, / and % run before + and -. Operators of the same level run from left to right, and parentheses change the order, as in math:
2 + 3 * 4is14;(2 + 3) * 4is20;20 / 3 * 2is12: first20 / 3gives6, then6 * 2.
Common mistake
The average of two integers is stored in a double in the hope of getting a fraction:
int total = 5;
int count = 2;
double average = total / count; // ✗
Console.WriteLine(average); // 2The compiler sees no error, and the program prints 2. C# first performs total / count as integer division, and only its result 2 turns into a double. Cast an operand to double, not the result: (double)total / count gives 2.5.
Key points
- Dividing two integers drops the fractional part:
17 / 5is3, and-17 / 5is-3. - The
%operator gives the remainder, and its sign matches the sign of the dividend. - The quotient and the remainder are linked:
a / b * b + a % balways equalsa. - To get a fraction, cast at least one operand to
double, not the result.