Start learning free

LESSON 22 OF 410 · CALCULATIONS WITH NUMBERS

Floor Division in Java

Lesson Read the full explanation

TL ; DR:

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

    System.out.println(Math.floor(3.9)); // Output: 3
    System.out.println(Math.floor(5.2)); // Output: 5
    System.out.println(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;
    
    System.out.println(Math.floor(20 / a)); // Output: 6
    System.out.println(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

    JAVA · CALCULATIONS WITH NUMBERS

    Floor Division in Java, explained

    Floor Division in Java: Integer / Truncates Toward Zero, Math.floorDiv() Actually Floors

    Java's integer division operator / does not floor toward negative infinity — it truncates toward zero. For positive numbers the result is identical, so this difference rarely surfaces until you work with negatives. Java 8 added Math.floorDiv() for true floor behavior that matches Python's //. Understanding the difference prevents subtle bugs in algorithms, circular indexing, and any calculation involving negative values.

    Integer / vs Math.floorDiv(): Side-by-Side

    // Positive numbers — identical results:
    System.out.println(10 / 3);                  // 3
    System.out.println(Math.floorDiv(10, 3));    // 3
    
    // Negative numbers — CRITICAL DIFFERENCE:
    System.out.println(-10 / 3);                 // -3  (truncates toward zero)
    System.out.println(Math.floorDiv(-10, 3));   // -4  (floors toward -∞)
    //  -10 / 3 = -3.333...
    //  truncate → -3  (toward zero)
    //  floor    → -4  (toward -∞, matching Python's -10 // 3)
    

    The Modulo Difference: % vs Math.floorMod()

    Java's % is a remainder operator (result sign matches dividend). Math.floorMod() gives Python-style modulo (result always non-negative when divisor is positive):

    // Positive: identical
    System.out.println(10 % 3);                  // 1
    System.out.println(Math.floorMod(10, 3));    // 1
    
    // Negative: different
    System.out.println(-10 % 3);                 // -1  (remainder, sign = dividend)
    System.out.println(Math.floorMod(-10, 3));   //  2  (modulo, sign = divisor)
    
    // Invariant holds for each pair:
    // a / b * b + a % b == a                              (truncation pair)
    // Math.floorDiv(a,b)*b + Math.floorMod(a,b) == a     (floor pair)
    int a = -10, b = 3;
    System.out.println(Math.floorDiv(a,b) * b + Math.floorMod(a,b) == a);  // true
    

    Quotient and Remainder for Positive Values

    // For positive numbers, plain / and % are fine:
    int dividend = 26;
    int divisor  = 3;
    int quotient  = dividend / divisor;   // 8
    int remainder = dividend % divisor;   // 2
    
    System.out.println("Quotient:  " + quotient);   // 8
    System.out.println("Remainder: " + remainder);  // 2
    System.out.println((quotient * divisor + remainder) == dividend);  // true
    

    Real-World Patterns

    // 1. Convert seconds to minutes and seconds:
    int totalSeconds = 137;
    int minutes = totalSeconds / 60;     // 2
    int seconds = totalSeconds % 60;     // 17
    System.out.printf("%dm %ds%n", minutes, seconds);  // "2m 17s"
    
    // 2. Binary search midpoint (avoids integer overflow):
    int left = 0, right = Integer.MAX_VALUE - 1;
    int mid = left + (right - left) / 2;   // safe midpoint
    
    // 3. Total pages needed (ceiling division trick):
    int totalItems = 95, perPage = 10;
    int pages = (totalItems + perPage - 1) / perPage;   // 10
    System.out.println(pages + " pages");
    
    // 4. Distribute items into rows:
    int items = 29, perRow = 4;
    int fullRows = items / perRow;    // 7
    int leftover = items % perRow;    // 1
    System.out.println(fullRows + " full rows, " + leftover + " left over");
    

    Math.floorDiv() for Negative-Safe Code (Java 8+)

    // Use Math.floorDiv() when inputs can be negative:
    int offset = -7;
    int period = 3;
    
    System.out.println(offset / period);               // -2  (truncation)
    System.out.println(Math.floorDiv(offset, period)); // -3  (floor — matches Python)
    
    // Circular array indexing — floorMod stays non-negative:
    int index = -1;
    int size  = 5;
    System.out.println(Math.floorMod(index, size));    // 4  (wraps to previous position)
    System.out.println(index % size);                  // -1  (not useful for indexing)
    

    Key Takeaways

    • Java's / on integers truncates toward zero — it does NOT floor. Use Math.floorDiv() for true floor behavior (Java 8+).
    • Java's % can return negative values when the dividend is negative — use Math.floorMod() for always-non-negative results.
    • For positive inputs only, / and % work exactly as expected.
    • Ceiling division trick: (a + b - 1) / b avoids floating-point entirely.

    What to Learn Next

    With integer division solid, explore Java's full Math class: Math.ceil() and Math.round() for floating-point rounding, Math.abs() for absolute value, and Math.pow() for exponentiation. Apply modulo to algorithm problems: checking even/odd (n % 2 == 0), circular array indexing (Math.floorMod(i, arr.length)), and FizzBuzz.

    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free