Quotient and remainder
When you divide one whole number by another, the quotient is how many times the divisor fits completely, and the remainder is what is left over. For example, 15 / 4 has quotient 3 and remainder 3, because 4 fits three times into 15 (12) with 3 left over.
Some languages have a separate "floor division" operator, often written //. C# does not. It does not need one, because dividing two integers already gives the quotient.
Integer division with /
In C#, the type of the operands decides what / does. If both are integers, the result is an integer and the fractional part is discarded:
Console.WriteLine(10 / 2); // 5
Console.WriteLine(15 / 4); // 3
Console.WriteLine(20 / 3); // 6
If at least one operand is a double, the result is a double and the fraction is kept:
Console.WriteLine(15 / 4.0); // 3.75
This works with variables too. With int a = 3; and int b = 7;, 20 / a is 6 and b / a is 2.
Truncation and flooring
C# integer division truncates toward zero: it removes the fraction rather than rounding down. For positive numbers these are the same. For negative numbers they are not: -7 / 2 is -3 in C#, because the exact value, -3.5, is cut toward zero. A true floor would give -4.
Math.Floor
Math.Floor returns the largest whole number that is less than or equal to its argument. It works on floating-point values and returns a double:
Console.WriteLine(Math.Floor(3.9)); // 3
Console.WriteLine(Math.Floor(5.2)); // 5
Console.WriteLine(Math.Floor(-3.5)); // -4
It is useful when you really need rounding down for negative values, for example Math.Floor(-7 / 2.0) is -4. Here the divisor is written 2.0 so the division happens in floating point before Math.Floor sees it.
A trap: Math.Floor on an int division
You may see code like Math.Floor(20 / a) in other languages. In C# the division in the parentheses is already an integer division, so the fraction is gone before Math.Floor runs, and the call adds nothing. Worse, Math.Floor has overloads for double and decimal, and an int argument fits both equally well, so Math.Floor(7) fails to compile with an ambiguity error. Cast one side first, as in Math.Floor((double)b / a), or simply use b / a when you want the integer quotient.
The exercise
A product costs price dollars (15) and Andy has amount dollars (70). He can't buy a fraction of a product, so you print how many he can afford. Mathematically 70 divided by 15 is about 4.67, and the answer is the whole part, 4.
Both variables are int, so amount / price already produces the quotient. No Math.Floor or cast is needed, because both values are positive and truncation matches rounding down.
The solution in C#
int amount = 70;
int price = 15;
Console.WriteLine(amount / price);
This prints 4.
Quotient and remainder together
The two results always fit back together: divisor * quotient + remainder equals the original number. For 70 and 15, the quotient is 4 and the remainder is 10, and 15 * 4 + 10 is 70. The remainder is found with the modulo operator, %, the subject of the next lesson. Together the two operators let you split any amount into whole groups and a leftover, which is a pattern that appears in many problems.
In the exercise, the leftover money is amount - 4 * price, which is 10 dollars, after Andy buys four products.
Common mistakes
- Converting to double. If you change a variable to
double, the result becomes4.666666666666667, which is not a count of whole products. - Dividing by zero. Integer division by zero throws a
DivideByZeroExceptionat run time. Adoubledivision by zero givesInfinityinstead. - Swapping the operands.
price / amountis15 / 70, which is0, because 70 does not fit into 15 even once. Put the total on the left. - Expecting rounding.
19 / 10is1, not2. The fraction is dropped, never rounded up.
Key takeaways
/on two integers gives the quotient and drops the fraction.- It truncates toward zero, which differs from flooring for negative numbers.
Math.Floorworks ondoublevalues and returns adouble.