I need clarity on Modulus\remainder operator

I was busy with some mock exam & came across a modulus operator. I've always understood it when the numerator is bigger than the denominator, like 5%2 will give me 1.... but now I was faced with another one, the numerator was bigger... it was 1%3 and it gave me 1. In my mind I've made my own theories to understand this, but I think it is best to ask if you don't know.

how did 1%3 become 1?

Your help will be highly appreciated... I do not want to assume things

Re: I need clarity on Modulus\remainder operator

Ah the wonders of integer math. See it like this:

5/2 = 2, because 2 fits twice inside 5.

5%2 = 1, because 2 fits twice inside 5 and that has a *remainder of 1* (5 - 2 -2).

13/5 = 2 because 5 fits twice inside 13

13%5 = 3, because 5 fits twice inside 13 (total 10) and then there is 3 left over

1%3 = 1 because 3 fits zero times inside 1, which leaves a remainder of 1.

Note: done from memory, so I may have goofed somewhere with one of the numbers.

Re: I need clarity on Modulus\remainder operator

The modulus or modulo operator is simply to come up with the remainder of a division. So 1/3 gives you a quotient of 0, and the remainder of 1. Wikipedia at Modulo operation - Wikipedia, the free encyclopedia has a pretty robust description of this.

Re: I need clarity on Modulus\remainder operator

Thanx alot guys... and to add to your answers I've just found thisWhat is 1 mod 5? and 1 mod 3? - Yahoo Answers

Re: I need clarity on Modulus\remainder operator

Just be careful with the 1 mod 5 notation. That is very similar to number theory notation where the modulus can be many different values. In the JLS, % is actually referred to as the remainder operator. If you want the complete details, check it out here. Remainder Operator %

Regards,

Jim

Re: I need clarity on Modulus\remainder operator

The % operator is defined as: a%b == a-b*(a/b), where / is the integer division operator (this is also defined for negative numbers).

kind regards,

Jos

Re: I need clarity on Modulus\remainder operator

Isn't it funny how we ignore the JLS :(happy): I'm looking at it & I'm not sure if I'll be able to grasp all there is to know about remainder operator in one moment

I'm busy preparing for the OCAJP 7 exam which I plan to take on the 24th of this month

Thanks again to all of you

Re: I need clarity on Modulus\remainder operator

Quote:

Originally Posted by

**Kagiso** Isn't it funny how we ignore the JLS :(happy):

How strange, because I took my definition (see reply #6) straight from the JLS (see 15.17.3 Remainder Operator %).

kind regards,

Jos

Re: I need clarity on Modulus\remainder operator

Quote:

Originally Posted by

**JosAH** How strange, because I took my definition (see reply #6) straight from the JLS (see 15.17.3 Remainder Operator %).

kind regards,

Jos

What I was trying to say is... I didn't think of looking at the JLS until both you & Jim led me to it... English is not my language & I didn't mean to offend