DoAssignment study guide

A1.1 · Use integer division and remainders

Learn to use integer division and remainders through clear examples and targeted practice.

Ontario Grade 12 Computer Science

Programming Concepts and Skills

ICS4U A1.1 study lesson

This lesson focuses only on integer division and remainders. You have likely already divided whole numbers and worked with Python expressions. Here, you will connect those ideas: integer division gives the whole-number quotient, while the remainder records what is left over. The examples use a positive divisor and a non-negative dividend. This makes the results straightforward to interpret and matches the situations shown in the lesson.

What you will learn

  • Explain how integer division and remainders describe the division of whole numbers.
  • Use Python 3's // and % operators with positive integers.
  • Trace a short program and check that its quotient and remainder fit the input.

1. Review: division with whole numbers

With ordinary division, a result may include a fraction or decimal. Integer division instead asks how many complete groups of a given size fit into a quantity. For example, if 23 items are placed into groups of 5, four complete groups can be made, with 3 items left over.
The two answers are connected. Let n be a non-negative whole number and d be a positive whole number. Integer division gives q, and the remainder operation gives r. The input is n and d; the outputs are q and r. In Python 3, write n // d for q and n % d for r.
The governing rule is that the dividend equals the divisor multiplied by the quotient, plus the remainder. For a positive divisor, the remainder is at least zero and smaller than the divisor. These checks help you spot a mistaken calculation.
n=dq+r,0≤r<dn=dq+r,\quad 0\le r<d
  • Integer division counts complete groups.
  • The remainder is the amount left after making those groups.
  • For the examples here, the divisor is positive and the dividend is non-negative.

2. Python operators and a reliable trace

In Python 3, // is the integer-division operator and % is the remainder operator. For non-negative integers with a positive divisor, // returns the number of complete groups and % returns what remains. They can be used in separate expressions or assigned to separate variables.
Before tracing a calculation, identify the two inputs and check the preconditions: the values are integers, the dividend is non-negative, and the divisor is positive. Then work out how many complete groups fit. Multiply that count by the divisor and compare it with the dividend; the leftover amount is the remainder.
For example, with 29 and 6, four complete groups use 24, leaving 5. The quotient is therefore 4 and the remainder is 5. The remainder is smaller than 6, as the rule requires. The code below illustrates those operations; its expected output is derived by tracing the arithmetic, not by claiming the code was run.
q=n//d,r=n%dq=n\mathbin{//}d,\quad r=n\mathbin{\%}d
  • Use // to obtain the integer quotient.
  • Use % to obtain the remainder.
  • Check the quotient and remainder together against the original dividend.

3. Use the remainder to check the division

A quotient on its own does not say how much was left over. A remainder on its own does not say how many complete groups were made. Reading both results together gives a complete account of the division.
To check a result, multiply the quotient by the divisor and add the remainder. The total must equal the original dividend. Also check that the remainder is smaller than the divisor. For instance, if a proposed result for division by 7 had a remainder of 7, one more complete group would fit, so that quotient and remainder would not be in the required form.
These checks are useful when reading a program trace or calculating by hand. They do not require a new programming technique: they apply the same relationship between the input, quotient, divisor, and remainder.
dq+r=ndq+r=n
  • The quotient and remainder describe the same division.
  • Reconstruct the dividend to check a result.
  • A remainder equal to or larger than the divisor signals an incorrect quotient-remainder pair.

4. Practice and transfer

For each calculation, begin by naming the dividend and divisor. Find the greatest number of complete groups that does not exceed the dividend, then calculate what remains. Finally, check both parts using the governing rule.
In a short program, choose clear variable names for the quotient and remainder. The expected output should match the traced values. If it does not, revisit the inputs and the two operator results rather than guessing.
The worked examples below use different inputs to practise exact division, division with a remainder, and tracing both results from one pair of inputs.
  • State the inputs before calculating.
  • Trace each operator result.
  • Check the result using multiplication and addition.

Worked example

A. Groups with items left over

A class has 38 counters and makes groups of 6. Find the number of complete groups and the number of counters left over.

Python 3

items = 38
group_size = 6
print(items // group_size)
print(items % group_size)
  1. Identify the inputs
    The dividend is 38 counters, and the divisor is 6 counters per group. The inputs are non-negative integers and the divisor is positive.
    n=38,d=6n=38,\quad d=6
  2. Find complete groups
    Six groups use 36 counters. A fifth group would need 6 more, so only six complete groups fit.
    38//6=638\mathbin{//}6=6
  3. Find what remains
    Subtract the 36 counters used by the six groups from the original 38. The two remaining counters are fewer than 6.
    38%6=238\mathbin{\%}6=2
Answer: There are 6 complete groups and 2 counters left over.
Check: 6×6+2=386\times 6+2=38

Worked example

B. Exact division

A library places 56 books equally into stacks of 8. Find how many complete stacks can be made and how many books remain.

Python 3

books = 56
per_stack = 8
stacks = books // per_stack
leftover = books % per_stack
print(stacks)
print(leftover)
  1. Set the inputs
    The dividend is the number of books, and the divisor is the number of books per stack. Both inputs meet the stated preconditions.
    n=56,d=8n=56,\quad d=8
  2. Calculate the quotient
    Seven stacks contain 56 books, so the division is exact and no books are left over.
    56//8=756\mathbin{//}8=7
  3. Calculate the remainder
    Because the seven stacks use every book, the remainder is zero. The expected output of the displayed code is 7 followed by 0 on separate lines, based on this trace.
    56%8=056\mathbin{\%}8=0
Answer: The library can make 7 complete stacks, with 0 books left over.
Check: 7×8+0=567\times 8+0=56

Worked example

C. Trace both results

For a non-negative whole-number input of 47 and a positive divisor of 9, determine the values assigned to quotient and remainder.

Python 3

value = 47
divisor = 9
quotient = value // divisor
remainder = value % divisor
print(quotient)
print(remainder)
  1. Trace integer division
    Five complete groups of 9 use 45. A sixth group would require 54, which is more than the input, so the quotient is 5.
    47//9=547\mathbin{//}9=5
  2. Trace the remainder
    After the five groups use 45, two units remain. The remainder is less than the divisor of 9.
    47%9=247\mathbin{\%}9=2
  3. Check the pair
    Combining five groups of nine with the two remaining units reproduces the input. The expected printed values are 5 and 2, each on its own line.
    5×9+2=475\times 9+2=47
Answer: The quotient is 5 and the remainder is 2.
Check: The remainder is non-negative and smaller than the divisor: 2 is less than 9.

Common mistakes and how to avoid them

Treating // as ordinary division and expecting a decimal answer.
Correction: For the non-negative integer inputs in this lesson, // gives the number of complete groups as an integer.
Assuming % gives the quotient.
Correction: The % operator gives the remainder; // gives the quotient.
Accepting a remainder that is as large as the divisor.
Correction: Check that the remainder is smaller than the positive divisor. If it is not, another complete group can be made.
Checking only the quotient.
Correction: Check the quotient and remainder together by multiplying the quotient by the divisor and adding the remainder.

Lesson summary

  • For non-negative integer n and positive integer d, n // d gives the whole-number quotient.
  • For the same inputs, n % d gives the remainder.
  • The quotient and remainder satisfy n = d times q plus r, with r from 0 up to, but not including, d.
  • Trace both operator results and use the relationship to check them.

Check your understanding

Question 1

What are 31 // 4 and 31 % 4?
  1. 7 and 3
  2. 7 and 4
  3. 8 and 0
  4. 7.75 and 0
Show answer and explanation
7 and 3
Seven groups of 4 use 28, leaving 3. The remainder is less than 4.

Question 2

For 45 // 5 and 45 % 5, which pair is correct?
  1. 9 and 0
  2. 8 and 5
  3. 9 and 5
  4. 0 and 9
Show answer and explanation
9 and 0
Nine groups of 5 use all 45, so the quotient is 9 and the remainder is 0.

Question 3

Which statement correctly describes the % operator in the examples in this lesson?
  1. It gives the remainder after complete groups are counted.
  2. It gives the number of complete groups.
  3. It always gives a decimal result.
  4. It gives the original dividend unchanged.
Show answer and explanation
It gives the remainder after complete groups are counted.
The % operator returns the remainder; // returns the integer quotient.

Key terms

Dividend
The number being divided.
Divisor
The number that tells the size of each group.
Quotient
The number of complete groups; for the examples here, it is the result of Python's // operator.
Remainder
The amount left after making complete groups; for the examples here, it is the result of Python's % operator.

Continue through ICS4U

View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A1.1. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question