TExcelTAD
Home › Basic Math & Pre-Algebra › Division and Remainders

Division and Remainders

Division splits an amount into equal parts, or asks how many times one number fits into another. When it does not fit exactly, something is left over: the remainder. Programmers use remainders far more than most people expect, from paging through search results to deciding whose turn it is in a game.

Words and symbols

In 20÷4=520 \div 4 = 5, the number being divided (20) is the dividend, the number you divide by (4) is the divisor, and the result (5) is the quotient. Division is also written as a fraction, 204\frac{20}{4}, or with a slash in code: 20 / 4.

Two ways to picture 20÷420 \div 4:

  • Sharing: 20 sweets shared fairly between 4 children gives each child 5.
  • Grouping: how many groups of 4 can you make from 20? Five groups.

Division undoes multiplication: 20÷4=520 \div 4 = 5 because 5×4=205 \times 4 = 20. That is how you check a division.

When it doesn't divide evenly

23 sweets shared between 4 children: each gets 5 (that uses 20) and 3 are left. We write

23÷4=5 remainder 3because23=4×5+323 \div 4 = 5 \text{ remainder } 3 \quad\text{because}\quad 23 = 4 \times 5 + 3

This division equation is the key fact of the whole lesson:

dividend=divisor×quotient+remainder,with 0≤remainder<divisor\text{dividend} = \text{divisor} \times \text{quotient} + \text{remainder}, \qquad \text{with } 0 \le \text{remainder} < \text{divisor}

The remainder is always smaller than the divisor. If it were 4 or more, every child could get another sweet.

Python gives the quotient with //, the remainder with %, and both at once with divmod:

python · try it yourself
a, b = 23, 4
q, r = a // b, a % b
print("quotient:", q, " remainder:", r)
print("divmod:", divmod(a, b))
print("check:", b * q + r == a)
print("exact answer:", a / b)
quotient: 5  remainder: 3
divmod: (5, 3)
check: True
exact answer: 5.75

The exact answer 5.75 is the quotient plus the remainder shared out too: 5+345 + \frac{3}{4}.

Long division

For bigger numbers, long division works through the dividend one digit at a time, from the left: divide, multiply, subtract, bring down the next digit, repeat.

text
Divide 857 by 6:

  8 ÷ 6 = 1, 1 × 6 = 6,   8 − 6 = 2,   bring down 5 → 25
 25 ÷ 6 = 4, 4 × 6 = 24, 25 − 24 = 1,  bring down 7 → 17
 17 ÷ 6 = 2, 2 × 6 = 12, 17 − 12 = 5,  no digits left

quotient 142, remainder 5      check: 6 × 142 + 5 = 852 + 5 = 857 ✓

The same steps in code. Each step keeps a running remainder, multiplies it by 10 and adds the next digit, which is exactly "bring down":

python · try it yourself
def long_division(dividend, divisor):
    quotient_digits, rem = [], 0
    for ch in str(dividend):
        current = rem * 10 + int(ch)          # bring down the next digit
        digit = current // divisor
        rem = current - digit * divisor
        quotient_digits.append(str(digit))
        print(f"{current:3d} / {divisor} = {digit}, remainder {rem}")
    return int("".join(quotient_digits)), rem

print("result:", long_division(857, 6))
  8 / 6 = 1, remainder 2
 25 / 6 = 4, remainder 1
 17 / 6 = 2, remainder 5
result: (142, 5)

Dividing by zero

You can never divide by zero. 12÷012 \div 0 would have to be a number that gives 12 when multiplied by 0, and no such number exists, because anything×0=0\text{anything} \times 0 = 0. Python refuses with an error, and so does every language for integers:

python · try it yourself
try:
    print(12 / 0)
except ZeroDivisionError as e:
    print("error:", e)
print(0 / 12)       # zero divided by something is fine
error: division by zero
0.0

Zero divided by a number is fine: sharing 0 sweets among 12 children gives each child 0.

Rounding the quotient up

Sometimes the leftover still needs a whole unit. 95 search results at 10 per page need 10 pages, not 9: the last 5 results need a page of their own. This is ceiling division. In Python, -(-a // b) or math.ceil(a / b) both work; the first stays exact for huge integers.

python · try it yourself
import math

results, per_page = 95, 10
print("full pages:", results // per_page, " leftover:", results % per_page)
print("pages needed:", -(-results // per_page), math.ceil(results / per_page))
print("buses for 130 people, 48 seats each:", -(-130 // 48))
full pages: 9  leftover: 5
pages needed: 10 10
buses for 130 people, 48 seats each: 3

Note: With negative numbers, Python's // rounds down toward −∞-\infty: -7 // 2 is -4, and -7 % 2 is 1, so that the division equation still holds (2×−4+1=−72 \times -4 + 1 = -7). C and Java round toward zero instead (−3 remainder −1). The Modular Arithmetic lesson returns to this.

In software

  • Pagination and batching: number of pages = ceiling of items ÷\div page size.
  • Even and odd: n % 2 == 0 tests evenness; i % 3 == 0 does something every third time round a loop.
  • Turn order and wrap-around: in a 4-player game, the player after player p is (p + 1) % 4. An animation with 8 frames shows frame tick % 8.
  • Hash tables place a key in bucket hash % number_of_buckets (see Hash Tables).
  • Unit conversion: seconds to minutes and seconds is a divmod by 60.

Exercises

Exercise 1: Quotient and remainder

Find 100÷7100 \div 7 as a quotient and remainder, and write the division equation.

Answer

14 remainder 2, because 100=7×14+2100 = 7 \times 14 + 2.

Solution

7×14=987 \times 14 = 98 is the largest multiple of 7 not above 100, and 100−98=2100 - 98 = 2. The remainder 2 is less than 7, as it must be.

Exercise 2: Long division

Divide 4 738 by 9 with long division.

Answer

526 remainder 4

Solution
text
 4 ÷ 9 = 0, remainder 4,  bring down 7 → 47
47 ÷ 9 = 5, 45, remainder 2, bring down 3 → 23
23 ÷ 9 = 2, 18, remainder 5, bring down 8 → 58
58 ÷ 9 = 6, 54, remainder 4
quotient 0526 = 526, remainder 4;  check 9 × 526 + 4 = 4 734 + 4 = 4 738 ✓
python · try it yourself
print(divmod(4738, 9))
(526, 4)

Exercise 3: Boxes

Eggs are packed in boxes of 12. How many boxes are needed for 250 eggs, and how many eggs are in the last box?

Answer

21 boxes; the last box holds 10 eggs.

Solution

250=12×20+10250 = 12 \times 20 + 10. Twenty boxes are full and the 10 leftover eggs need a 21st box.

Exercise 4: Whose turn?

Five players sit in a circle numbered 0 to 4, and player 0 starts. Write code that prints who plays turns 0 to 11 using %. Who plays turn 11?

Answer

Player 1 plays turn 11 (11 mod 5=111 \bmod 5 = 1).

Solution

Turn tt belongs to player t mod 5t \bmod 5: after player 4 it wraps back to 0.

python · try it yourself
players = 5
print([turn % players for turn in range(12)])
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1]

Exercise 5: Time conversion

Convert 10 000 seconds into hours, minutes and seconds using divmod twice.

Answer

2 h 46 min 40 s

Solution
10 000÷60=166 min remainder 40 s166÷60=2 h remainder 46 min\begin{aligned} 10\,000 \div 60 &= 166\ \text{min remainder } 40\ \text{s} \\ 166 \div 60 &= 2\ \text{h remainder } 46\ \text{min} \end{aligned}
python · try it yourself
minutes, seconds = divmod(10000, 60)
hours, minutes = divmod(minutes, 60)
print(hours, "h", minutes, "min", seconds, "s")
2 h 46 min 40 s

Test yourself

What is 47 % 6 in Python?

  1. 7
  2. 5
  3. 1
  4. 7.83

A ferry carries 35 cars per trip. How many trips are needed for 140 cars? For 141 cars?

  1. 4 and 4
  2. 4 and 5
  3. 5 and 5
  4. 3 and 4

Which statement about division by zero is correct?

  1. 12÷0=012 \div 0 = 0
  2. 12÷0=1212 \div 0 = 12
  3. 12÷012 \div 0 is undefined, but 0÷12=00 \div 12 = 0
  4. Both 12÷012 \div 0 and 0÷120 \div 12 are undefined