n%m means n modulo m (abbreviated n mod m)
n%2 means n modulo 2
n%3 means n modulo 3
The modulo is simply the remainder, so if the modulo is 0, the number n is exactly divisible by m. If n is not divisible by m, then there is a remainder.
Hence, (6%2) equals 0, (5%2) equals 1, (5%3) equals 2 etc. To clarify the example above by TonyGroves:
Code:
int n=123;
if (n%2) // literally means if (n%2 != 0)
printf ("odd");
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.