tag:blogger.com,1999:blog-694855878384792308.post820537320922716977..comments2021-08-03T03:21:15.177-07:00Comments on Java67: How to use Modulo , Modulus, or Remainder Operator in Java? [Example]javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-694855878384792308.post-76235389741910778092021-01-27T20:30:50.057-08:002021-01-27T20:30:50.057-08:00Hello Anonymous, very interesting comment. I think...Hello Anonymous, very interesting comment. I think you are right that they are actually remainder operator which is also loosely called modulus operator but it doesn't really mean modulus operator of Maths which you explained here. Its also interesting to know that Lisp and Ada has both. Thxjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-15490858424185969062021-01-22T09:06:46.164-08:002021-01-22T09:06:46.164-08:00Technically (n % m) is a remainder operator, but n...Technically (n % m) is a remainder operator, but not a modulus operator. There's a difference.<br /><br />For nonnegative n and positive m, the remainder and modulus are the same thing. But for negative n, they are different. -2 mod 10 is defined to be 8 in standard mathematics, going back centuries. But the remainder of -2 over 10 is -2. So (-2 % 10) gives -2, which is the remainder. It doesn't give 8, which is the mod.<br /><br />If you ever need an actual mod operator, then (((n % m) + m) % m) gives the mod. In most cases where you have a negative n, you'll actually need to find the mod rather than the remainder. So it's unfortunate Java doesn't have a mod operator built in. It inherited this from C. <br /><br />I wish C had defined % to be remainder and %% to be mod. That would have allowed us to avoid having to use ugly constructions like (((n % m) + m) % m) when we need the mod. <br /><br />Some languages actually do have both. For example, LISP has both "mod" and "rem" as operators. So does Ada. But sadly, C and all its descendants have only rem, not mod.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-63138219738917969702020-04-17T02:53:05.087-07:002020-04-17T02:53:05.087-07:00What is mod here? are you using remainder operator...What is mod here? are you using remainder operator in Java. do you mean 39.03/9 ?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-31740007449314105442020-04-16T19:27:52.820-07:002020-04-16T19:27:52.820-07:0039.02 mod 9 = 3.020000000000003
Can anyone explai...39.02 mod 9 = 3.020000000000003<br /><br />Can anyone explain? I thought it would be only 3.02.Anonymoushttps://www.blogger.com/profile/07677529817672113071noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-39598243290466420022017-02-02T02:14:23.146-08:002017-02-02T02:14:23.146-08:00public static void main(String[] args) {
/... public static void main(String[] args) {<br /> // TODO code application logic here<br /> int n,dec=0,i=0,N=0;<br /> Scanner in=new Scanner(System.in);<br /> System.out.println("Enter number in binary form like 0 1 or 1 0");<br /> n=in.nextInt();<br /> while(n > 0)<br /> <br /> {<br /> dec = n % 10;<br /> N =N+ dec *( int)Math.pow(2,i);<br /> i++;<br /> n=n/10;<br /> }<br /> System.out.println("Decimal \t"+N);<br /> }<br />}<br /> Can you explain me the dry run of above program when binay is 11.manish kumarhttps://www.blogger.com/profile/15448506003749786267noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-64585193385375809562016-06-26T05:02:04.022-07:002016-06-26T05:02:04.022-07:00Hello @Subhrajit, 20%10 will return zero and that&...Hello @Subhrajit, 20%10 will return zero and that's your last digit, isn't it? javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-52342995326263843002016-06-26T05:01:20.148-07:002016-06-26T05:01:20.148-07:00@Anonymous, good point, modulo operator was indeed...@Anonymous, good point, modulo operator was indeed the simplest of hash functionjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-28073662458599268622016-06-26T03:08:27.417-07:002016-06-26T03:08:27.417-07:00If user enters a digit With o at end like 20,30 th...If user enters a digit With o at end like 20,30 then how will the use of mod give the last digit<br /><br />Anonymoushttps://www.blogger.com/profile/16896852928294839364noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-29272468573599438362015-11-28T15:50:13.174-08:002015-11-28T15:50:13.174-08:00What about the other way around, when the dividend...What about the other way around, when the dividend is smaller than the divisor?<br />int result = (-5) % 3; // gives -2.<br />int result = (-3) % 5; // gives -3.<br />In general, int result = (-a) % b; gives the right answer when |-a| > b.<br />In order to get the proper result when |-a| < b we should wrap the divisor.<br />int result = ((-a) % b) + b; for negative a or int result = (((-a) % b) + b) % b; for positive or negative a<br />http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3Oz Edrihttps://www.blogger.com/profile/10188708197129293579noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-68193545467769537262015-01-21T02:01:04.861-08:002015-01-21T02:01:04.861-08:00Thanks for listing these interesting use of modulu...Thanks for listing these interesting use of modulus operator. One more I can add is on implementing hash function on hash table to convert hash keys into array indexes. For example, if your hash table is backed by an array or size 10, and you want to store arbitrary integers on this table, you need to convert those numbers into valid array index e.g. 0 to 9. We can use modulus operaotr here. <br /><br />for example, if you want to store 56 in this table, 56%9 will give you 2, so you can store 56 there. When you want it back, use the same hash function again. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-57105511825627801252014-12-10T22:30:39.752-08:002014-12-10T22:30:39.752-08:00Though you have already listed couple of interesti...Though you have already listed couple of interesting examples of remainder operator in Java, I would like to add few more :<br />1) to check if number is even or odd, if(number%2) == 0 then number is even<br />2) get the last digit in any base, for example in decimal, number%10 will return last digit, in binary number%2 will return last bit, in octal number%8 will return last octat and in hexadecmial number%16 should return last digit.<br />3) You can use remainder operator to get every nth count e.g.<br />if ( everyFiftCustomer++ % 5 == 0 ) {<br /> giveDiscount();<br />}<br /><br />4) You can check if an year is leap year or not by using following logic<br />if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)<br /> leap year<br />else <br /> not a leap year<br /><br />5) Used in circular array and hashtable to conain elements in limit.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-5311324450914035762014-12-10T22:23:17.958-08:002014-12-10T22:23:17.958-08:00One of the most important thing to remember about ...One of the most important thing to remember about modulo operator in Java is how it works with negative operands. For example, what do you think following operations will print :<br /><br />int mod = -20 % 3;<br />System.out.println("value of -20 % 3 = " + mod);<br /> <br />mod = 20 % (-3);<br />System.out.println("value of 20 % (-3) = " + mod);<br /> <br />mod = -20 % (-3);<br />System.out.println("value of -20 % (-3) = " + mod);<br /><br />when you run these snippets, you will get something like <br />value of -20 % 3 = -2<br />value of 20 % (-3) = 2<br />value of -20 % (-3) = -2<br /><br />do you understand why? as many of you might have expected +2 in last operation and -2 in second operation.<br />well, Java's modulo or mod or remainder operator uses sign of dividend (top operand) and not of divisor (bottom operand).<br /><br />Which means, if dividend is negative then remainder will be negative and if dividend is positive then result would be positive, irrespective of sign of divisor. this also explains the output, because you get negative remainder in first case because dividend was negative, second result was positive again because dividend was positive and third result is negative irrespective of divisor is also negative. Remember this concept, if you are going for SCJP or OCAJP, many programmer get it wrong.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-74241577496220888542014-12-10T22:10:22.029-08:002014-12-10T22:10:22.029-08:00Can we use modulo operator with float or double in...Can we use modulo operator with float or double in Java also? Anonymousnoreply@blogger.com