Practice mode C#

What does Console.WriteLine(-7 % 3); print in C#?

Correct answer B. -1

Explanation

In C#, the result of the remainder operator % takes the sign of the left operand (the dividend). -7 / 3 truncates to -2, and -7 - (-2 × 3) = -1. Some languages such as Python return 2 here because they use floored division, which is why this catches people out. To get a non-negative result, use ((a % n) + n) % n.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.