How modulus operation works in C++? [closed]

The answer is correct. ‘%’ mean “reminder”. The % operator is remainder operator. The A % B operator actually answer the question “If I divided A by B using integer arithmetic, what would the remainder be?”

dividend = quotient * divisor + remainder

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
.....
etc..

For negative number…

   1 % (-4) = 1
(-2) % 4    = -2
(-3) % (-4) = -3

With a remainder operator, the sign of the result is the same as the sign of the dividend

you can read more at What’s the difference between “mod” and “remainder”?

Browse More Popular Posts

Leave a Comment