{"id":23225,"date":"2023-12-01T11:34:15","date_gmt":"2023-12-01T11:34:15","guid":{"rendered":"https:\/\/linuxways.net\/?p=23225"},"modified":"2024-03-25T02:10:29","modified_gmt":"2024-03-25T02:10:29","slug":"modulus-c-language","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/modulus-c-language\/","title":{"rendered":"Modulus in C Language"},"content":{"rendered":"<div id=\"wpbody\">\nIn this Linux Ways article, you will learn how to get the remainder after a division between two variables. We will show you the easiest way to get a module using the \u201c%\u201d operator and also the mathematical formula to get it by applying it, so you can see the different possibilities you have to solve the remainder of a division.<\/p>\n<p>We&#8217;ll then put what we learned into practice with several practical examples using the code and images, so you&#8217;ll understand how each method works and be able to use them quickly.<\/p>\n<h2><a id=\"post-23225-_7wcm6vmpt6r0\"><\/a>Module or Remainder After Division<\/h2>\n<p>The rule for exact division of integers is that the divisor must be a multiple of the dividend. When we work with integers and perform the division operations, in cases where the dividend is not divisible by the divisor, the result is an integer approximation. From this division, we are left with a remainder that is less than the divisor.<\/p>\n<p>This remainder, also called modulus, affects the accuracy of the operations because, as we already said, the result of a division that has a remainder is only an integer approximation.<\/p>\n<h2><a id=\"post-23225-_j16s488u21ou\"><\/a>Formula to Determine the Modulus of a Division<\/h2>\n<p>One way to get the modulus of a division is to subtract the result of the division from the dividend multiplied by the divisor. The following is the formula to obtain the modulus of \u201ca\u201d divided by \u201cb\u201d using this method:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">mod <span class=\"sy0\">=<\/span> a <span class=\"sy0\">-<\/span><span class=\"br0\">&#40;<\/span>a<span class=\"sy0\">\/<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">*<\/span>b<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Now, let&#8217;s see how to apply this formula in a simple example that gives the modulus of the division of 27 divided by 5:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">27<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> b <span class=\"sy0\">=<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> d<span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> mod<span class=\"sy0\">;<\/span><br \/>\nd <span class=\"sy0\">=<\/span> a<span class=\"sy0\">\/<\/span>b<span class=\"sy0\">;<\/span><br \/>\n<br \/>\nmod <span class=\"sy0\">=<\/span> a <span class=\"sy0\">-<\/span><span class=\"br0\">&#40;<\/span>a<span class=\"sy0\">\/<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">*<\/span>b<span class=\"sy0\">;<\/span> <span class=\"co1\">\/\/Get module<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;a \/ b = %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> d<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp;<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;mod = %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> mod<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following is the execution of this code where we can see the result of \u201ca\u201d divided by \u201cb\u201d or the remainder of the division of 27 over 5:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23232\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23225-_gf1x1dy6wkxv\"><\/a>The \u201c%\u201d Operator to Obtain the Module of a Division<\/h2>\n<p>The C language provides the \u201c%\u201d operator to obtain the modulus in a division. Because of its simple syntax, the use of this operator to obtain the modulus of a division is the most commonly used in the C language. The following is the expression that is used to obtain the modulus of \u201ca\u201d divided by \u201cb\u201d:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">mod <span class=\"sy0\">=<\/span> a <span class=\"sy0\">%<\/span> b<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>NOTE: The \u201c%\u201d operator that is used under this expression refers to a mathematical operator that has nothing to do with the format identifiers that are used in the strings of the printf() function to specify the output format.<\/p>\n<h2><a id=\"post-23225-_wdw8ka3ckr77\"><\/a>How to Get the Modulus of a Division with the \u201c%\u201d Operator<\/h2>\n<p>In this example, we will show you how to use the \u201c%\u201d operator to get the modulus of a division. To do this, we specify the integer that we want the dividend to be and give it a random odd value.<\/p>\n<p>We also declare the \u201cb\u201d integer which will be the divisor that we will assign with a random even value. We store the result in the \u201cmod\u201d integer which we then output to the screen using the printf() function.<\/p>\n<p>The next step is to apply the operation with the \u201c%\u201d operator to the \u201ca\u201d and \u201cb\u201d variables and display the result in the command console. Let\u2019s see the following code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">int<\/span> a <span class=\"sy0\">=<\/span><span class=\"nu0\">27<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> b <span class=\"sy0\">=<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> mod<span class=\"sy0\">;<\/span><br \/>\n<br \/>\nmod <span class=\"sy0\">=<\/span> a <span class=\"sy0\">-<\/span><span class=\"br0\">&#40;<\/span>a<span class=\"sy0\">\/<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">*<\/span>b<span class=\"sy0\">;<\/span><br \/>\n&nbsp;<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;mod = %i<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> mod<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image illustrates the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23233\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see in the figure, the division of an odd number by an even number is not exact, which is why it resulted in a remainder or modulus which, in this case, is equal to 3.<\/p>\n<h2><a id=\"post-23225-_i07he4kgyu45\"><\/a>How to Know If a Number Is a Multiple of Another with the \u201c%\u201d Operator<\/h2>\n<p>The \u201c%\u201d operator allows us to solve various programming tasks such as determining whether one value is a multiple of another. To do this, we simply perform a modulo operation where a result that is equal to 0 means that the divisor is a multiple of the dividend.<\/p>\n<p>In the following, we will see an example that lists the values from 0 to 255, which are multiples of 8, using a modulo operation and an \u201cif\u201d condition to determine them:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">int<\/span> mod<span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> a<span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> a <span class=\"sy0\">!=<\/span> <span class=\"nu0\">255<\/span><span class=\"sy0\">;<\/span> a<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;mod <span class=\"sy0\">=<\/span> a <span class=\"sy0\">%<\/span> <span class=\"nu0\">8<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>mod <span class=\"sy0\">==<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%i Is multiple of 8<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the list of multiples of 8 which is generated with the \u201c%\u201d operator and the \u201cif\u201d condition:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23234\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23225-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23225-_6ewed1tvoa3y\"><\/a>Conclusion<\/h2>\n<p>In this Linux Ways article, we showed you how to get the module of a division with two different modes. We explained how to get a modulus by a simple operation with the \u201c%\u201d operator. To see the different ways to solve the same problem in this language, we also showed you the applied equation which is used for this purpose.<\/p>\n<p>Also, as an example, we showed you how the \u201c%\u201d operator can be used to solve various problems such as finding the multiples of a given value using the simple conditionals and module operations.\n<\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>Comprehensive tutorial on how to get the module of a division with two different modes and how to get a modulus by a simple operation with the \u201c%\u201d operator.<\/p>","protected":false},"author":110,"featured_media":23235,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23225","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23225"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23225\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23235"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}