% or the modulus operator basically is this p % n can be looked at like this divide p by n and then the remainder is given.
or we can write it like this
1 2
|
while( p > n )
p -= n;
|
Basically say we have 27 % 2 this would mean
27 / 2 = 13 / 2 = 6/2 = 3/2 = 1
Basically we divide then round down since it takes int not double
Another method would be to do
1 2 3 4
|
while( p > n )
{
p /= n;
}
|
Basically dividing and subtracting is the same if you think about it. and an int divided by an int is an int and not a double.
What I mean by dividing and subtracting being the same is that think about it
12 / 4
=
12 - 4 = 8 -> 1
8 - 4 = 4 -> 2
4 - 4 = 0 -> 3
answer is 3 ( we subtracted as many times as we could )
This is how int divisions work
[edit]if you did 12 % 4 that 0 that is left over would be the result.[/edit]
Lets look at another
15 / 2
15 - 2 = 13 -> 1
13 - 2 = 11 -> 2
11 - 2 = 9 -> 3
9 - 2 = 7 -> 4
7 - 2 = 5 -> 5
5 - 2 = 3 -> 6
3 - 2 = 1 -> 7
1 is less than 2 so answer would be 7
[edit]if you did 15 % 2 that 1 that is left over would be the result.[/edit]
Anyways hopefully I didn't confuse you but % operator means just divide as many times as you can then return the remainder.
http://www.cprogramming.com/tutorial/modulus.html
http://www.cplusplus.com/doc/tutorial/operators/
http://stackoverflow.com/questions/12556946/how-does-the-modulus-operator-work