Results 1 to 9 of 9
 03142014, 01:17 PM #1Member
 Join Date
 Nov 2012
 Location
 Johannesburg, South Africa
 Posts
 92
 Rep Power
 0
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
 03142014, 01:38 PM #2Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 4,937
 Rep Power
 7
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."Syntactic sugar causes cancer of the semicolon."  Alan Perlis
 03142014, 01:41 PM #3Senior Member
 Join Date
 Feb 2014
 Posts
 219
 Rep Power
 2
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.
 03142014, 02:25 PM #4Member
 Join Date
 Nov 2012
 Location
 Johannesburg, South Africa
 Posts
 92
 Rep Power
 0
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
 03142014, 03:52 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,630
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03142014, 06:49 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,003
 Blog Entries
 7
 Rep Power
 23
Re: I need clarity on Modulus\remainder operator
The % operator is defined as: a%b == ab*(a/b), where / is the integer division operator (this is also defined for negative numbers).
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 03142014, 10:33 PM #7Member
 Join Date
 Nov 2012
 Location
 Johannesburg, South Africa
 Posts
 92
 Rep Power
 0
Re: I need clarity on Modulus\remainder operator
Isn't it funny how we ignore the JLS 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
 03152014, 08:36 AM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,003
 Blog Entries
 7
 Rep Power
 23
 03152014, 12:42 PM #9Member
 Join Date
 Nov 2012
 Location
 Johannesburg, South Africa
 Posts
 92
 Rep Power
 0
Similar Threads

Unary operator and binary operator
By suhaas.mohandos@gmail.com in forum New To JavaReplies: 10Last Post: 08272013, 04:39 PM 
Question about how Modulus works
By calistoknight in forum New To JavaReplies: 2Last Post: 09242012, 12:06 PM 
multiplication using increment operator (with out arthematic operator)
By mallikanala in forum New To JavaReplies: 5Last Post: 01222012, 12:02 AM 
modulus value keeps returning a zero value
By leoshiner in forum New To JavaReplies: 3Last Post: 10242010, 04:11 PM 
[SOLVED] Modulus/Remainder results
By antgaudi in forum New To JavaReplies: 9Last Post: 10132008, 03:49 AM
Bookmarks