How does modulus of a smaller dividend and larger divisor work?

Viewed 22968
7 % 3 = 1 (remainder 1)

how does
3 % 7 (remainder ?)

work?

11 Answers

Conceptually, I think of it this way. By definition, your dividend must be equal to (quotient * divisor) + modulus

Or, solving for modulus: modulus = dividend - (quotient * divisor)

Whenever the dividend is less than the divisor, the quotient is always zero which results in the modulus simply being equal to the dividend.

To illustrate with OP's values:

modulus of 3 and 7 = 3 - (0 * 7) = 3

To illustrate with other values:

1 % 3:
1 - (0 * 3) = 1

2 % 3:
2 - (0 * 3) = 2

It seems you forgot to mention the surprising case, if the divident is smaller and negative:

-3 % 7 

result: 4

The most simple and effective catch to remember would be: Whenever dividend is less than the divisor, modulus is just that dividend.

Let's formulate this:

if x < y, then x % y = x 
Related