Pascal modulo with negative divisor: If A = 20 and B = -7 in Pascal, what is the value of A mod B?

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:

  • A = 20, B = -7.
  • Pascal's 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:

Compute q = 20 div (−7) = −2.Compute r = 20 − (−7) * (−2) = 20 − 14 = 6.Therefore, 20 mod (−7) = 6.


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:

2, 3: do not satisfy the identity with the correct truncation behavior.−1: remainder should keep magnitude less than |b| and satisfy the equality; −1 fails with q = −2.


Common Pitfalls:

Assuming Python-style floor division or C's implementation specifics; languages differ in div/mod rules with negatives.


Final Answer:

6

More Questions from Microprocessors

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion