# Modulo Operation - %

#1

I just did the chapter 7 loops challenge but I needed a little help at the end regarding the modulo operation or the ‘%’. I kind of get what it does.

The code from the challenge I’m referring to is:

``````   if (i % 5 == 5){
printf("Found one! %d\n",i);
continue;
}
else if (i % 5 == 0){
printf("Found one! %d\n",i);
continue;``````

I get that if you take the number and divide it by 5 and it has no remainders it MIGHT be divisible by 5 but I don’t get how a number with a remainder of 5 is divisible by 5 also. Does this just mean that if a number is 10 or 15 or 20 or 25 its remainder is 0 or 5? I think of remainders as decimals.

Just a little confused on this, thanks for any clarification

#2

Don’t think of remainders in terms of decimals.

Do you believe this?
13 % 5 is 3. (Because 5 x 2 + 3 = 13)

If you mod something by 5, the result will always be 0, 1, 2, 3, or 4. (Never 5)

If (y % 5) == 0, y MUST be divisible by 5.

#3

But its mod basically dision so wouldnt 13 % 5 be 13 / 5 (which is 2.3). Im confused where the 5x2+3=13 is coming from.

#4

13 divided by 5 is 2 with a remainder of 3.

#5

Or more formally:

Given an integer N (N <= 0 or N >= 0) and a positive integer M (M > 0)

N can be written as N = k * M + m, where k is an integer (k<= 0 or k >= 0), and m is an integer (m >= 0 and m < M)

Then N modulo M = m

Thus 13 modulo 5 = 3 because 13 = 2 * 5 + 3 where N = 13, k = 2, M = 5, m = 3

Thus -17 modulo 8 = 7 because -17 = -3 * 8 + 7

By the way % operator is not really the modulo operator; it is the remainder operator, which is not the same as the modulo operator according to the above definition.

#6

Thank you guys for clearing that up!