BuildQuill
C# learning path
3 of 25
Lesson 3 of 25
Lesson 3Beginner15 min

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

  • Variables
  • int, double, and decimal

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.

01

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.

C#
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
02

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.

C#
int completedTasks = 9;
int days = 4;

Console.WriteLine(completedTasks / days);          // 2
Console.WriteLine((double)completedTasks / days);  // 2.25
03

Precedence, 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.

ExpressionMeaningResult from 10
score += 3Add 3 and store it13
score -= 2Subtract 2 and store it8
score *= 4Multiply by 4 and store it40
score++Add exactly 111
04

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.

C#
decimal rawAverage = 10m / 3m;
decimal shownAverage = Math.Round(rawAverage, 2);

Console.WriteLine(rawAverage);
Console.WriteLine(shownAverage);

Good habits

  • Write units into names when confusion is possible: durationMinutes is clearer than duration.
  • Convert before division, not after a fractional part has already been discarded.
  • Do not compare floating-point measurements for exact equality unless exact representation is guaranteed.

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.

Practice challenge

Now build it without copying.

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.

You are done when

  • The tip rate is represented as a fractional decimal
  • Division keeps the fractional part
  • Intermediate values have descriptive names

Stretch: Use Math.Round for the per-person amount and calculate any remainder left after multiplying it back by the diner count.

Open challenge in playground

Lesson checkpoint

One small step locks it in

Mark this lesson complete, then keep the momentum going.

Complete and continue

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.