What is Integer division?
Division of integers that produces an integer quotient. How the fractional part is handled differs between languages: some truncate toward zero (C, Java), and others round down toward negative infinity (Python's //). The results are the same for positive numbers but differ for negative ones.
Japanese name: 整数除算
Integer division appears in 4 practice questions across 3 topics, including Computer Arithmetic, Programming Basics (Pseudocode Tracing), C Programming Basics.
Field: Programming Basics (Pseudocode Tracing)
Differences with negative numbers
Dividing −7 by 2 gives a quotient of −3 and a remainder of −1 in C and Java, which truncate toward zero. Python's //, which rounds down, gives a quotient of −4, and % gives a remainder of 1. In both, "quotient × divisor + remainder = dividend" holds; the remainder takes the sign of the dividend in C and the sign of the divisor in Python. This causes mistakes when programs that handle negative numbers are rewritten in another language.
FAQ
What happens when dividing by zero?
Integer division by zero is a runtime error (an exception) in many languages and undefined behavior in C. Programs should check that the divisor is not zero before dividing.
How are the quotient and remainder of integer division related?
With quotient q and remainder r, dividend = q × divisor + r. The quotient and remainder are defined as a pair, so if the rounding of the quotient changes (truncation or rounding down), the remainder changes too.