Beginner trap · integer math

Why is 7 / 2 = 3 in C#?

It's not a bug and it's not rounding. When both sides of / are integers, C# performs integer division: it computes 3.5 and throws the fraction away. Every snippet here runs live — check it yourself.

Console.WriteLine(7 / 2);          // int / int   → int  (3)
Console.WriteLine(7 / 2.0);        // int / double → double
Console.WriteLine(7.0 / 2);        // double / int → double

The rule: if either operand is a double, the other is promoted and the result keeps its fraction. If both are integers (int, long, short…), the result is an integer and the fraction is discarded — always toward zero, never "rounded".

The hidden-variable version (the one that bites)

Literals are obvious. The bug arrives when the double is hiding inside a variable's declared type:

int a = 7;
Console.WriteLine(a / 2);      // a is an int → integer division → 3

double x = 7;                  // same value, different type
Console.WriteLine(x / 2);      // now double / int → 3.5

This is why "it worked in my test with 7 / 2.0 and breaks with user input" happens: Console.ReadLine() gives you a string, and if you int.Parse it, you've opted into integer math for everything downstream.

Truncation, not rounding — and negatives go toward zero

Console.WriteLine(7 / 2);    //  3.5 →  3  (fraction dropped)
Console.WriteLine(-7 / 2);   // -3.5 → -3  (toward zero, NOT toward -4)
Console.WriteLine(9 / 2);    //  4.5 →  4
Console.WriteLine(-9 / 2);   // -4.5 → -4

C# truncates toward zero. If you actually want rounding, say so explicitly:

Console.WriteLine(Math.Round(7 / 2.0));   // 3.5 → 4  (banker's rounding)
Console.WriteLine((int)Math.Ceiling(7 / 2.0)); // always up → 4

The three fixes

Fix 1: make one operand a double (most idiomatic)

int a = 7, b = 2;
double result = a / (double)b;
Console.WriteLine(result);

Casting just one side is enough — the promotion spreads through the whole expression. a / (double)b and (double)a / b both work.

Fix 2: multiply by 1.0 (quick and dirty)

int a = 7, b = 2;
Console.WriteLine(1.0 * a / b);   // 1.0 * a is double → everything promotes

Fix 3: declare the variables as double from the start

If the values are quantities that can be fractional (prices, averages, measurements), they probably should be double:

double a = 7, b = 2;
Console.WriteLine(a / b);

The related trap: % keeps the wrong-looking sign

The remainder operator follows the same integer rules, and in C# it takes the dividend's sign:

Console.WriteLine(7 % 3);    //  1
Console.WriteLine(-7 % 3);   // -1  (sign follows -7)
Console.WriteLine(7 % -3);   //  1  (sign follows 7)

When integer division is exactly what you want

It's not a design flaw — it's how you split things into pages, buckets and groups:

int totalItems = 98;
int pageSize = 10;

int fullPages = totalItems / pageSize;      // 9 full pages
int lastPageItems = totalItems % pageSize;  // 8 leftovers
Console.WriteLine((fullPages + (lastPageItems > 0 ? 1 : 0)) + " pages total");
Console.WriteLine(pageSize);

Related reading: the cast that quietly eats your fraction when you assign a double to an int is error CS0029 / CS0266. Try int x = 3.5; in the playground and see what the compiler says.