Start learning free

LESSON 22 OF 409 · CALCULATIONS WITH NUMBERS

Floor Division in C++

Lesson Read the full explanation

TL ; DR:

  • Remember that in C++, division between integers with / returns only the integral part of the result:

    cout << 10 / 2 << endl; // Output: 5
    cout << 15 / 4 << endl; // Output: 3
    cout << 20 / 3 << endl; // Output: 6
    
    int a = 3;
    int b = 7;
    
    cout << 20 / a << endl; // Output: 6
    cout << b / a << endl; // 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

    Floor Division in C++: Integer / Truncates Toward Zero, Not Toward -∞

    C++ integer division with / truncates toward zero — it does not floor toward negative infinity. For positive numbers this is identical to floor division, so the difference is invisible until negative numbers appear. For float floor division, use std::floor() from <cmath>. Understanding this prevents subtle off-by-one bugs in algorithms, circular indexing, and anything involving negative offsets.

    Integer /: Truncation Toward Zero (C++11 Guaranteed)

    // Positive: integer / gives the integer quotient directly
    std::cout << 10 / 3  << "\n";   // 3
    std::cout << 15 / 4  << "\n";   // 3
    std::cout << 20 / 3  << "\n";   // 6
    
    // Negative: truncates toward ZERO, not toward -∞
    std::cout << -10 / 3  << "\n";  // -3  (truncation: -3.33 → -3)
    // Python: -10 // 3 = -4          (floor:       -3.33 → -4)
    // C++ truncates; Python floors — they differ for negative operands
    

    std::floor() for True Floor Division

    To get floor semantics (round toward −∞), use std::floor() from <cmath> for floats, or a helper for integers:

    #include <cmath>
    
    // True floor division for integers:
    int floorDiv(int a, int b) {
        int q = a / b;                          // truncates toward zero
        // If signs differ and there's a remainder, subtract 1 to floor
        if ((a ^ b) < 0 && q * b != a) q--;
        return q;
    }
    
    std::cout << 10 / 3           << "\n";   // 3  (truncation)
    std::cout << floorDiv(10, 3)  << "\n";   // 3  (floor — same for positives)
    std::cout << -10 / 3           << "\n";  // -3 (truncation)
    std::cout << floorDiv(-10, 3)  << "\n";  // -4 (floor — matches Python)
    
    // For floats, std::floor() directly:
    std::cout << std::floor(-10.0 / 3) << "\n";  // -4.0
    

    The % Operator: Remainder, Not Modulo

    C++'s % is a remainder operator — its sign follows the dividend (like Java, unlike Python):

    // Positive: fine
    std::cout << 10 % 3   << "\n";   // 1
    
    // Negative dividend: remainder is negative
    std::cout << -10 % 3  << "\n";   // -1  (C++ — sign follows -10)
    // Python: -10 % 3 = 2             (Python — sign follows 3)
    
    // Python-style always-non-negative modulo in C++:
    auto pyMod = [](int a, int b) { return ((a % b) + b) % b; };
    std::cout << pyMod(-10, 3) << "\n";  // 2 — matches Python
    

    Quotient and Remainder (Positive Values)

    #include <iostream>
    using namespace std;
    
    int main() {
        int dividend = 26;
        int divisor  = 3;
    
        int quotient  = dividend / divisor;   // 8
        int remainder = dividend % divisor;   // 2
    
        cout << "Quotient:  " << quotient  << "\n";   // 8
        cout << "Remainder: " << remainder << "\n";   // 2
        cout << (quotient * divisor + remainder == dividend) << "\n";  // 1 (true)
    
        return 0;
    }
    

    Real-World Patterns

    // 1. Convert seconds to minutes and seconds:
    int totalSeconds = 137;
    int minutes = totalSeconds / 60;   // 2
    int seconds = totalSeconds % 60;   // 17
    cout << minutes << "m " << seconds << "s\n";   // "2m 17s"
    
    // 2. Binary search midpoint (no overflow):
    int left = 0, right = INT_MAX - 1;
    int mid = left + (right - left) / 2;
    
    // 3. Total pages needed (ceiling division):
    int totalItems = 95, perPage = 10;
    int pages = (totalItems + perPage - 1) / perPage;   // 10
    cout << pages << " pages\n";
    
    // 4. Distribute items into rows:
    int items = 29, perRow = 4;
    int fullRows = items / perRow;   // 7
    int leftover = items % perRow;   // 1
    cout << fullRows << " full rows, " << leftover << " left over\n";
    

    Key Takeaways

    • C++ integer / truncates toward zero — not floor. Differs from Python's // for negative operands (C++11 guarantees this).
    • For true floor division, use std::floor(a / b) for floats, or implement a floorDiv() helper for integers.
    • C++ % can be negative when the dividend is negative — use ((a % b) + b) % b for always-non-negative modulo.
    • For positive inputs only, plain / and % work exactly as expected.

    What to Learn Next

    With integer division solid, explore C++'s <cmath> functions: std::ceil() for ceiling division, std::round() for nearest-integer rounding, and std::abs() for absolute value. Apply modulo to algorithm problems: checking even/odd (n % 2 == 0), circular array indexing (with the pyMod pattern above), and hash table implementations where negative indices must wrap correctly.

    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