Arithmetic and integer division
Arithmetic operators let a program calculate quantities from existing values. The types of the operands matter: dividing two integers produces an integer quotient, while dividing decimal operands can preserve a fractional part. Use quotient and remainder together when packing items into groups.
int books = 17;int size = 5;System.Console.WriteLine(books / size);System.Console.WriteLine(books % size);Three cartons are full and two books remain.
Expected output (reasoned, not executed):
32Calculate with numeric operators
+, -, *, / and % perform addition, subtraction, multiplication, division and remainder. For positive integer counts, n / size counts full groups and n % size counts items left over. Integer division truncates toward zero, so -17 / 5 is -3 and -17 % 5 is -2. An integer divisor of zero is invalid at runtime (or a compile error in a constant expression). Also guard the signed minimum divided by -1: the mathematical quotient does not fit in that signed type. Do not assume unchecked division makes this overflow safe. The positive packing examples avoid this edge case.
Precedence and updates
Multiplication, division and remainder bind more tightly than addition and subtraction. Parentheses make intended grouping explicit. count += 2 updates count; count++ increments it by one. Prefix ++count produces the updated value; postfix count++ produces the previous value while still updating count. Prefer separate updates when the returned value is unnecessary.
Choose the calculation type before dividing
A cast such as (decimal)books converts the value before the division. The m suffix makes a decimal literal. Assigning an integer division result to decimal afterward cannot recover the lost fraction. For nonnegative counts, a remainder greater than zero means one extra carton is needed; decisions are taught in chapter 5.
Examples and walkthroughs
decimal average = 17m / 5;System.Console.WriteLine(average.ToString(System.Globalization.CultureInfo.InvariantCulture));System.Console.WriteLine(17 / 5);The first calculation uses decimal arithmetic and invariant formatting (a fixed culture). The second uses integer arithmetic.
Expected output (reasoned, not executed):
3.43int count = 4;System.Console.WriteLine(count++);System.Console.WriteLine(count);System.Console.WriteLine(2 + 3 * 4);System.Console.WriteLine((2 + 3) * 4);Postfix increment returns the old count. Parentheses alter grouping, not the multiplication rule.
Expected output (reasoned, not executed):
451420Common mistake and correction
Writing decimal average = 17 / 5; stores 3, not 3.4. Convert an operand first. Also avoid using % as a mathematical nonnegative modulo operation when negative inputs are possible.
Try it yourself
Pack 29 books into cartons of 6. Print full cartons and leftover books. Then calculate the decimal ratio for 9 books across 4 shelves.
Next: Comparisons and short-circuit logic. These examples target C# 14 / net10.0 and are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arithmetic and integer division — knowledge check
Apply the lesson concepts to the following scenarios.
What is 23 / 4 with int operands?
What is 23 % 4?
What is -9 / 4?
Which expression preserves the fraction?
After int n = 3; int old = n++; what are old and n?