Difficulty: Medium
Correct Answer: 6
Explanation:
Introduction / Context:
Different languages define integer division and modulo differently for negative operands. Pascal defines mod
via the identity a = (a div b) * b + (a mod b)
, with div
truncating toward zero in standard Pascal implementations.
Given Data / Assumptions:
div
truncates toward zero (implementation-typical).a mod b = a − b * (a div b)
.
Concept / Approach:
Compute a div b
first, then use the defining identity to find a mod b
. With truncation toward zero, 20 div −7 = −2 (since −2.857… truncated toward zero is −2). Plugging into the mod identity gives the remainder.
Step-by-Step Solution:
Verification / Alternative check:
Check identity: (−2) * (−7) + 6 = 14 + 6 = 20. The remainder's magnitude is less than |b| = 7, which is consistent.
Why Other Options Are Wrong:
Common Pitfalls:
Final Answer:
int
range of −32768 to +32767)?
Discussion & Comments