C# Numbers and Operators
Turn numeric values into reliable calculations and learn where integer division, rounding, precedence, and overflow can surprise you.
Before this lesson
Use arithmetic and assignment operators
Predict integer division and operator precedence
Choose explicit rounding and protect calculations from invalid assumptions
The short answer
C# arithmetic operators combine numeric values: +, -, *, /, and %. The operand types affect the result, so 5 / 2 is 2 with integers but 2.5 when at least one operand is a fractional type.
Operators describe a calculation
An operator is a symbol that asks C# to perform an operation on values. The values around it are operands. In price * quantity, the multiplication operator combines two operands and produces a new value; it does not alter either original variable unless you assign the result somewhere.
The remainder operator % returns what is left after whole-number division. 17 % 5 is 2. It is useful for testing even numbers, wrapping a position around a repeating range, or splitting a count into full groups and leftovers.
using System;
class Program
{
static void Main()
{
int minutes = 135;
int hours = minutes / 60;
int remainingMinutes = minutes % 60;
Console.WriteLine($"{hours} hours and {remainingMinutes} minutes");
}
}Expected output
2 hours and 15 minutes
The type controls division
When both operands are integers, C# performs integer division and discards the fractional part. This is not rounding: 9 / 4 becomes 2 whether the discarded fraction is small or large. If a fractional result matters, make at least one operand a double or decimal before division.
This matters in averages. int average = total / count silently loses the fraction before it reaches average. Writing double average = (double)total / count converts total first, so the division can produce a fraction.
int completedTasks = 9;
int days = 4;
Console.WriteLine(completedTasks / days); // 2
Console.WriteLine((double)completedTasks / days); // 2.25Precedence, parentheses, and updates
C# follows precedence rules: multiplication, division, and remainder happen before addition and subtraction. Parentheses run their inner expression first and make intended grouping visible. Even when the language would calculate the same result without them, parentheses can save the next reader from re-deriving the rule.
Compound assignment operators update a variable from its current value. score += 5 means score = score + 5. Increment and decrement, score++ and score--, change an integer by one. Prefer the form that makes the business operation obvious; compact syntax is not automatically clearer.
| Expression | Meaning | Result from 10 |
|---|---|---|
| score += 3 | Add 3 and store it | 13 |
| score -= 2 | Subtract 2 and store it | 8 |
| score *= 4 | Multiply by 4 and store it | 40 |
| score++ | Add exactly 1 | 11 |
Rounding and numeric boundaries are decisions
Displaying two decimal places does not change the stored value. When a rule actually requires rounding, call Math.Round and choose the precision deliberately. Financial and regulatory rules can specify a midpoint strategy, so do not assume every domain wants the same behavior.
Numeric types also have limits. An int cannot hold a value above int.MaxValue. In a checked context, overflow throws an exception instead of wrapping silently. Most beginner programs will not reach those boundaries, but counters, identifiers, and large multiplications should use a type chosen for their possible range.
decimal rawAverage = 10m / 3m;
decimal shownAverage = Math.Round(rawAverage, 2);
Console.WriteLine(rawAverage);
Console.WriteLine(shownAverage);Quick knowledge check
Answer before you reveal.
01What is the result of 7 / 2 when both values are int?
3. Integer division discards the fractional part.
02Does :0.00 in interpolation round the variable itself?
No. It formats the displayed representation; the stored numeric value is unchanged.
Exercise
Practice challenge
Calculate a restaurant bill from a meal price, tip percentage, and number of diners. Print the subtotal, tip, total, and each person's share to two decimal places.
Requirements
- The tip rate is represented as a fractional decimal
- Division keeps the fractional part
- Intermediate values have descriptive names
Optional extension: Use Math.Round for the per-person amount and calculate any remainder left after multiplying it back by the diner count.
Open in C# compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
When should I use decimal instead of double?
Use decimal when base-10 precision and domain rules matter, especially money. Use double for most measurements and scientific calculations where its range and performance fit better.
What does % mean with negative numbers?
It is the remainder operator, and the result follows the sign of the left operand in C#. If you need a always-positive wrap operation, normalize the result deliberately.
Is ++ better than += 1?
They express the same simple increment in common use. Choose the form that reads most clearly in context and avoid combining increments inside larger expressions.