ProgrammingHardTCS NQT · Infosys

int a = -7, m = 5; What is ((a % m) + m) % m, the non-negative residue of a modulo m?

  1. A. 3
  2. B. -2
  3. C. 8
  4. D. 4

Correct answer: A. 3

In C, % keeps the sign of the dividend, so -7 % 5 is -2. Adding 5 gives 3, and reducing again keeps 3. Using the raw -2 is the usual mistake in competitive code.

More C Programming questions

Exams that ask this topic