Concepts / Floor Division and Integer Division

Floor Division and Integer Division

The modulus operator (%) returns the remainder when one integer is divided by another.

  • Programming

From Groups to Leftovers

When you divide objects into equal-sized groups, division gives you two useful pieces of information: how many complete groups you can make and how many items remain. The floor division operator // gives the number of complete groups. The modulus operator % gives the leftover, called the remainder.

In the real world

Suppose you have 7 apples and place them into groups of 3. You can make 2 complete groups, using 6 apples, and 1 apple remains ungrouped. The complete-group result is 2, while the remainder is 1.

7 apples2 groups of 31 applecomplete groupsremainder
How are seven items represented when they are divided into groups of three?

Reading the Operators

Python uses a % b for modulus, where a is the dividend and b is the divisor. The expression asks what remains after dividing a by b. Floor division, written a // b, asks how many complete groups of size b fit into a. True division, written a / b, gives the decimal result. These operators examine the same division situation but return different information.

//complete groupscomplete groups%leftoverremainder/decimal resultdecimal resultreturnsreturnsreturns
What information does each division operator return?

Tracing a Remainder

What do you think happens?

Predict the result of 17 % 5 before checking the explanation.

  • 2

  • 3

  • 5

  • 0

Reveal answer
Answer:

2

Three complete groups of 5 use 15 items. Two items remain, so the remainder is 2.

17 items3 groups of 515 used2 leftmake complete groups3 times 517 minus 15
How can you find the remainder by separating complete groups from leftover items?
python

The expression 17 % 5 returns 2 because 5 fits completely into 17 three times, accounting for 15 items, and 2 items remain. The corresponding floor division expression 17 // 5 returns 3 because it counts the complete groups.

Repeating Remainder Patterns

With the same positive divisor, remainders stay within a fixed range. For a divisor b, the result is between 0 and b minus 1. If the dividend divides evenly by the divisor, the result is 0. If the dividend is smaller than the divisor, no complete group can be made, so the remainder is the dividend itself.

0remainder1remainder2remainder0cycle repeatsnext dividendnext dividendnext dividend
How do remainders repeat when increasing numbers are divided by three?
ExpressionRemainderWhat it shows
0 % 30The cycle starts at 0
1 % 31The remainder increases
2 % 32The largest possible remainder for divisor 3
3 % 30The cycle returns to 0
4 % 31The cycle repeats

Remainders repeat through 0, 1, and 2 when the divisor is 3.

This repeating behavior makes modulus useful for detecting patterns, checking divisibility, identifying even and odd numbers, distributing items into buckets, calculating positions in circular structures such as clocks or calendars, and working with recurring cycles.

Common Misunderstandings

  • Common mistake

    Treating % as another way to ask for the number of complete groups

    What it looks like

    Using 17 % 5 when the required result is the number of complete groups

    Why it fails

    The modulus operator returns what remains after complete groups are made. It does not return the number of complete groups.

    Do this instead

    Use // for the number of complete groups and % for the leftover.

  • Common mistake

    Expecting a remainder of 0 whenever the dividend is smaller than the divisor

    What it looks like

    Assuming 5 % 8 is 0

    Why it fails

    When the dividend is smaller than the divisor, no complete group can be made, but the entire dividend remains.

    Do this instead

    Recognize that 5 % 8 is 5.

  • Common mistake

    Forgetting that an even division produces a remainder of 0

    What it looks like

    Assuming every modulus expression produces a positive leftover

    Why it fails

    If the dividend divides evenly by the divisor, nothing is left over.

    Do this instead

    Check whether the divisor fits an exact number of times; an exact division has remainder 0.

  • Common mistake

    Confusing %, //, and /

    What it looks like

    Using / when the program needs either complete groups or a remainder

    Why it fails

    True division gives the decimal result, while floor division gives complete groups and modulus gives the remainder.

    Do this instead

    Choose the operator according to the information the program needs.

Practice Predictions

EASY

Predict the result of each modulus expression, then explain the complete groups and leftover items: 12 % 4, 10 % 3, 5 % 8, and 14 % 5.

Hints
  • Find how many complete groups of the divisor fit into the dividend.
  • Subtract the items in those complete groups from the original dividend.
  • Remember that an exact division has remainder 0.
  • When the dividend is smaller than the divisor, the dividend remains.
Worked example

Checking 14 % 5

Determine the remainder when 14 is divided by 5.

  1. 1
    Find complete groups

    Five fits into fourteen two complete times.

  2. 2
    Account for used items

    Two groups of five account for ten items.

  3. 3
    Find what remains

    Four items remain after ten are grouped.

Result

14 % 5 is 4.

Key Takeaways

  1. 1
    The modulus operator % returns the remainder after one integer is divided by another.
  2. 2
    The floor division operator // returns the number of complete groups.
  3. 3
    For a positive divisor b, a % b produces a value from 0 through b minus 1.
  4. 4
    An exact division has remainder 0, while a smaller dividend remains unchanged as the remainder.
  5. 5
    Repeating remainders make modulus useful for divisibility checks, even and odd patterns, buckets, positions, and recurring cycles.

At a glance

  • Modulus finds the leftover from division.
  • Floor division finds the number of complete groups.
  • Remainders follow predictable cycles for a fixed divisor.
  • Use modulus when a program needs to detect divisibility, repeating positions, or recurring patterns.

Related concepts

More concepts starting with F