Start learning free

LESSON 22 OF 347 · CALCULATIONS WITH NUMBERS

Floor Division in C#: Integer Division and Math.Floor

Lesson Read the full explanation

TL ; DR:

  • The Math.Floor() function returns the largest integer less than or equal to a given number:

    Console.WriteLine(Math.Floor(3.9)); // Output: 3
    Console.WriteLine(Math.Floor(5.2)); // Output: 5
    Console.WriteLine(Math.Floor(7)); // Output: 7
    
  • We can use this function if we want to compute the integral part of some division (quotient):

    int a = 3;
    int b = 7;
    
    Console.WriteLine(Math.Floor(20 / a)); // Output: 6
    Console.WriteLine(Math.Floor(b / a)); // Output: 2
    


Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

Floor division

Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6

It can also be used with variables:

a = 3
b = 7

print(20 // a) # Output: 6
print(b // a) # Output: 2

Modulo

The modulo operator (%) calculates the remainder of dividing two values:

print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2

# Can be used with variables:
a = 2
b = 4

print(b % a) # Output: 0
print(11 % b) # Output: 3

Quotient and remainder

In programming, we combine both these concepts to get the quotient and remainder of some divison:

# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3

print(quotient) # Output: 8
print(remainder) # Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Floor Division in C#, explained

    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 becomes 4.666666666666667, which is not a count of whole products.
    • Dividing by zero. Integer division by zero throws a DivideByZeroException at run time. A double division by zero gives Infinity instead.
    • Swapping the operands. price / amount is 15 / 70, which is 0, because 70 does not fit into 15 even once. Put the total on the left.
    • Expecting rounding. 19 / 10 is 1, not 2. 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.Floor works on double values and returns a double.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free