{"id":15542,"date":"2020-03-09T18:24:12","date_gmt":"2020-03-09T10:24:12","guid":{"rendered":"https:\/\/mkyong.com\/?p=15542"},"modified":"2020-03-09T18:24:12","modified_gmt":"2020-03-09T10:24:12","slug":"java-mod-examples","status":"publish","type":"post","link":"https:\/\/mkyong.com\/java\/java-mod-examples\/","title":{"rendered":"Java mod examples"},"content":{"rendered":"<p>Both remainder and modulo are two similar operations; they act the same when the numbers are positive but much differently when the numbers are negative. In Java, we can use <code>Math.floorMod()<\/code> to describe a modulo (or modulus) operation and <code>%<\/code> operator for the remainder operation.<\/p>\n<p>See the result:<\/p>\n<pre><code class=\"language-bash\">\r\n| rem &amp; +divisor| rem &amp; -divisor | mod &amp; +divisor | mod &amp; -divisor |\r\n| :-------------| :------------- | :------------- | :--------------|\r\n| -5 rem 3 = -2 | -5 rem -3 = -2 | -5 mod 3 =  1  | -5 mod -3 = -2 |\r\n| -4 rem 3 = -1 | -4 rem -3 = -1 | -4 mod 3 =  2  | -4 mod -3 = -1 |\r\n| -3 rem 3 =  0 | -3 rem -3 =  0 | -3 mod 3 =  0  | -3 mod -3 =  0 |\r\n| -2 rem 3 = -2 | -2 rem -3 = -2 | -2 mod 3 =  1  | -2 mod -3 = -2 |\r\n| -1 rem 3 = -1 | -1 rem -3 = -1 | -1 mod 3 =  2  | -1 mod -3 = -1 |\r\n|  0 rem 3 =  0 |  0 rem -3 =  0 |  0 mod 3 =  0  |  0 mod -3 =  0 |\r\n|  1 rem 3 =  1 |  1 rem -3 =  1 |  1 mod 3 =  1  |  1 mod -3 = -2 |\r\n|  2 rem 3 =  2 |  2 rem -3 =  2 |  2 mod 3 =  2  |  2 mod -3 = -1 |\r\n|  3 rem 3 =  0 |  3 rem -3 =  0 |  3 mod 3 =  0  |  3 mod -3 =  0 |\r\n|  4 rem 3 =  1 |  4 rem -3 =  1 |  4 mod 3 =  1  |  4 mod -3 = -2 |\r\n|  5 rem 3 =  2 |  5 rem -3 =  2 |  5 mod 3 =  2  |  5 mod -3 = -1 |\r\n<\/code><\/pre>\n<p>In a nutshell:<\/p>\n<ul>\n<li>`Remainder (rem)&#8220; = The result has the same sign (+ or -) as the dividend (first operand).<\/li>\n<li>`Modulo (mod)&#8220; = the result has the same sign (+ or -) as the divisor (second operand).<\/li>\n<\/ul>\n<p>**Further Reading- **<a href=\"https:\/\/en.wikipedia.org\/wiki\/Modulo_operation\" target=\"_blank\" rel=\"noopener\">Wikipedia &#8211; Modulo Operation<\/a><\/p>\n<h2>1. Remainder vs Modulo<\/h2>\n<p>1.1 Remainder Operator <code>%<\/code><\/p>\n<pre><code class=\"language-bash\">\r\ndividend rem divisor = remainder\r\n\r\n8 % 3 = 2\r\n\r\ndividend(8) rem(%) divisor(3) = remainder(2)\r\n<\/code><\/pre>\n<p>Let see an example to print each remainder from dividend -10 to 10 and a divisor of 3.<\/p>\n<div class=\"filename\">JavaModExample1.java<\/div>\n<pre><code class=\"language-java\">\r\npackage com.mkyong;\r\n\r\nimport java.util.Arrays;\r\nimport java.util.List;\r\n\r\npublic class JavaModExample1 {\r\n\r\n    public static void main(String[] args) {\r\n\r\n        int divisor = 3;\r\n\r\n        List&lt;Integer&gt; list = Arrays.asList(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);\r\n\r\n        String format = &quot;%3d rem %1d = %2d&quot;;\r\n        for (Integer dividend : list) {\r\n            String result = String.format(format, dividend, divisor, dividend % divisor);\r\n            System.out.println(result);\r\n        }\r\n\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n<p>Output<\/p>\n<pre><code class=\"language-bash\">\r\n-10 rem 3 = -1\r\n -9 rem 3 =  0\r\n -8 rem 3 = -2\r\n -7 rem 3 = -1\r\n -6 rem 3 =  0\r\n -5 rem 3 = -2\r\n -4 rem 3 = -1\r\n -3 rem 3 =  0\r\n -2 rem 3 = -2\r\n -1 rem 3 = -1\r\n  0 rem 3 =  0\r\n  1 rem 3 =  1\r\n  2 rem 3 =  2\r\n  3 rem 3 =  0\r\n  4 rem 3 =  1\r\n  5 rem 3 =  2\r\n  6 rem 3 =  0\r\n  7 rem 3 =  1\r\n  8 rem 3 =  2\r\n  9 rem 3 =  0\r\n 10 rem 3 =  1\r\n<\/code><\/pre>\n<p>For <code>divisor = -3<\/code><\/p>\n<pre><code class=\"language-bash\">\r\n-10 rem -3 = -1\r\n -9 rem -3 =  0\r\n -8 rem -3 = -2\r\n -7 rem -3 = -1\r\n -6 rem -3 =  0\r\n -5 rem -3 = -2\r\n -4 rem -3 = -1\r\n -3 rem -3 =  0\r\n -2 rem -3 = -2\r\n -1 rem -3 = -1\r\n  0 rem -3 =  0\r\n  1 rem -3 =  1\r\n  2 rem -3 =  2\r\n  3 rem -3 =  0\r\n  4 rem -3 =  1\r\n  5 rem -3 =  2\r\n  6 rem -3 =  0\r\n  7 rem -3 =  1\r\n  8 rem -3 =  2\r\n  9 rem -3 =  0\r\n 10 rem -3 =  1\r\n<\/code><\/pre>\n<p>With the remainder <code>%<\/code>, the result has the same sign as the dividend (first operand).<\/p>\n<p>1.2 Modulo with <code>Math.floorMod<\/code><\/p>\n<pre><code class=\"language-bash\">\r\ndividend mod divisor = modulus\r\n\r\n8 mod 3 = 2\r\n\r\nMath.floorMod(8, 2) = 2\r\n\r\ndividend(8) mod(%) divisor(3) = modulus(2)\r\n<\/code><\/pre>\n<p>Let see an example to print each modulo from dividend -10 to 10 and a divisor of 3.<\/p>\n<div class=\"filename\">JavaModExample2.java<\/div>\n<pre><code class=\"language-java\">\r\npackage com.mkyong;\r\n\r\nimport java.util.Arrays;\r\nimport java.util.List;\r\n\r\npublic class JavaModExample2 {\r\n\r\n    public static void main(String[] args) {\r\n\r\n        int divisor = 3;\r\n\r\n        List&lt;Integer&gt; list = Arrays.asList(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);\r\n\r\n        String format = &quot;%3d mod %1d = %2d&quot;;\r\n        for (Integer dividend : list) {\r\n            String result = String.format(format, dividend, divisor, Math.floorMod(dividend,divisor));\r\n            System.out.println(result);\r\n        }\r\n\r\n    }\r\n\r\n}\r\n\r\n<\/code><\/pre>\n<p>Output<\/p>\n<pre><code class=\"language-bash\">\r\n-10 mod 3 =  2\r\n -9 mod 3 =  0\r\n -8 mod 3 =  1\r\n -7 mod 3 =  2\r\n -6 mod 3 =  0\r\n -5 mod 3 =  1\r\n -4 mod 3 =  2\r\n -3 mod 3 =  0\r\n -2 mod 3 =  1\r\n -1 mod 3 =  2\r\n  0 mod 3 =  0\r\n  1 mod 3 =  1\r\n  2 mod 3 =  2\r\n  3 mod 3 =  0\r\n  4 mod 3 =  1\r\n  5 mod 3 =  2\r\n  6 mod 3 =  0\r\n  7 mod 3 =  1\r\n  8 mod 3 =  2\r\n  9 mod 3 =  0\r\n 10 mod 3 =  1\r\n<\/code><\/pre>\n<p>For <code>divisor = -3<\/code><\/p>\n<pre><code class=\"language-bash\">\r\n-10 mod -3 = -1\r\n -9 mod -3 =  0\r\n -8 mod -3 = -2\r\n -7 mod -3 = -1\r\n -6 mod -3 =  0\r\n -5 mod -3 = -2\r\n -4 mod -3 = -1\r\n -3 mod -3 =  0\r\n -2 mod -3 = -2\r\n -1 mod -3 = -1\r\n  0 mod -3 =  0\r\n  1 mod -3 = -2\r\n  2 mod -3 = -1\r\n  3 mod -3 =  0\r\n  4 mod -3 = -2\r\n  5 mod -3 = -1\r\n  6 mod -3 =  0\r\n  7 mod -3 = -2\r\n  8 mod -3 = -1\r\n  9 mod -3 =  0\r\n 10 mod -3 = -2\r\n<\/code><\/pre>\n<p>With the modulo, the result has the same sign as the divisor (second operand).<\/p>\n<h2>2. Common pitfalls &#8211; Find Odd numbers.<\/h2>\n<p>This example uses <code>%<\/code> remainder to check if a given number is an odd number. For negative numbers, it can lead to an unexpected results.<\/p>\n<div class=\"filename\">JavaModExample3.java<\/div>\n<pre><code class=\"language-java\">\r\npackage com.mkyong;\r\n\r\nimport java.util.Arrays;\r\nimport java.util.List;\r\n\r\npublic class JavaModExample3 {\r\n\r\n    public static void main(String[] args) {\r\n\r\n        int divisor = 2;\r\n        List&lt;Integer&gt; list = Arrays.asList(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);\r\n\r\n        String format = &quot;%3d %% %2d = %2d, isOdd() = %6b&quot;;\r\n        for (Integer dividend : list) {\r\n            String result = String.format(format, dividend, divisor, dividend % divisor, isOdd(dividend));\r\n            System.out.println(result);\r\n        }\r\n\r\n    }\r\n\r\n    \/\/ How about negative numbers?\r\n    private static boolean isOdd(int number) {\r\n        return number % 2 == 1;\r\n    }\r\n\r\n}\r\n\r\n<\/code><\/pre>\n<p>Output<\/p>\n<pre><code class=\"language-bash\">\r\n-10 %  2 =  0, isOdd() =  false\r\n -9 %  2 = -1, isOdd() =  false\r\n -8 %  2 =  0, isOdd() =  false\r\n -7 %  2 = -1, isOdd() =  false\r\n -6 %  2 =  0, isOdd() =  false\r\n -5 %  2 = -1, isOdd() =  false\r\n -4 %  2 =  0, isOdd() =  false\r\n -3 %  2 = -1, isOdd() =  false\r\n -2 %  2 =  0, isOdd() =  false\r\n -1 %  2 = -1, isOdd() =  false\r\n  0 %  2 =  0, isOdd() =  false\r\n  1 %  2 =  1, isOdd() =   true\r\n  2 %  2 =  0, isOdd() =  false\r\n  3 %  2 =  1, isOdd() =   true\r\n  4 %  2 =  0, isOdd() =  false\r\n  5 %  2 =  1, isOdd() =   true\r\n  6 %  2 =  0, isOdd() =  false\r\n  7 %  2 =  1, isOdd() =   true\r\n  8 %  2 =  0, isOdd() =  false\r\n  9 %  2 =  1, isOdd() =   true\r\n 10 %  2 =  0, isOdd() =  false\r\n<\/code><\/pre>\n<p>For <code>%<\/code> remainder operator, the result has the sign (+ or -) of dividends (first operand). For example <code>-3 % 2 = -1<\/code>, the above <code>isOdd()<\/code> will return <code>false<\/code>.<\/p>\n<p>One of the possible answers is test if the remainder is not equal to zero ( 0 has no sign (+ or -)).<\/p>\n<pre><code class=\"language-java\">\r\n  private static boolean isOdd(int number) {\r\n        return number % 2 != 0;\r\n    }\r\n<\/code><\/pre>\n<p>Output<\/p>\n<pre><code class=\"language-bash\">\r\n-10 %  2 =  0, isOdd() =  false\r\n -9 %  2 = -1, isOdd() =   true\r\n -8 %  2 =  0, isOdd() =  false\r\n -7 %  2 = -1, isOdd() =   true\r\n -6 %  2 =  0, isOdd() =  false\r\n -5 %  2 = -1, isOdd() =   true\r\n -4 %  2 =  0, isOdd() =  false\r\n -3 %  2 = -1, isOdd() =   true\r\n -2 %  2 =  0, isOdd() =  false\r\n -1 %  2 = -1, isOdd() =   true\r\n  0 %  2 =  0, isOdd() =  false\r\n  1 %  2 =  1, isOdd() =   true\r\n  2 %  2 =  0, isOdd() =  false\r\n  3 %  2 =  1, isOdd() =   true\r\n  4 %  2 =  0, isOdd() =  false\r\n  5 %  2 =  1, isOdd() =   true\r\n  6 %  2 =  0, isOdd() =  false\r\n  7 %  2 =  1, isOdd() =   true\r\n  8 %  2 =  0, isOdd() =  false\r\n  9 %  2 =  1, isOdd() =   true\r\n 10 %  2 =  0, isOdd() =  false\r\n<\/code><\/pre>\n<div class=\"note\">\n<strong>Note<\/strong><br \/>\nWhich one to choose, remainder <code>%<\/code> or modulo <code>Math.floorMod<\/code>? It depends on what you are going to build; both act the same for positive numbers but remember to take care of the negative result to avoid the common pitfalls like the <code>isOdd()<\/code> above.\n<\/div>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Modulo_operation\" target=\"_blank\" rel=\"noopener\">Wikipedia &#8211; Modulo Operation<\/a><\/li>\n<li><a href=\"https:\/\/www.mathsisfun.com\/numbers\/division-remainder.html\" target=\"_blank\" rel=\"noopener\">Division and Remainders<\/a><\/li>\n<li><a href=\"https:\/\/rob.conery.io\/2018\/08\/21\/mod-and-remainder-are-not-the-same\/\" target=\"_blank\" rel=\"noopener\">Mod and remainder are not the same<\/a><\/li>\n<li><a href=\"https:\/\/exploringjs.com\/deep-js\/ch_remainder-vs-modulo.html\" target=\"_blank\" rel=\"noopener\">% is a remainder operator, not a modulo operator<\/a><\/li>\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Math.html#floorMod-int-int-\" target=\"_blank\" rel=\"noopener\">Math.floorMod JavaDoc<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Both remainder and modulo are two similar operations; they act the same when the numbers are positive but much differently when the numbers are negative. In Java, we can use Math.floorMod() to describe a modulo (or modulus) operation and % operator for the remainder operation. See the result: | rem &amp; +divisor| rem &amp; -divisor &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Java mod examples\" class=\"read-more button\" href=\"https:\/\/mkyong.com\/java\/java-mod-examples\/#more-15542\" aria-label=\"Read more about Java mod examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":11208,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[1819,1343,1306,1845,1846,1847],"class_list":["post-15542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-interview-question","tag-java","tag-math","tag-mod","tag-modulo","tag-remainder","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/posts\/15542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/comments?post=15542"}],"version-history":[{"count":7,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/posts\/15542\/revisions"}],"predecessor-version":[{"id":15549,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/posts\/15542\/revisions\/15549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/media\/11208"}],"wp:attachment":[{"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/media?parent=15542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/categories?post=15542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mkyong.com\/wp-json\/wp\/v2\/tags?post=15542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}