{"id":25826,"date":"2017-10-25T20:54:34","date_gmt":"2017-10-25T15:24:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25826"},"modified":"2017-10-25T20:54:34","modified_gmt":"2017-10-25T15:24:34","slug":"smallest-three-integers-without-comparison-operators","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/smallest-three-integers-without-comparison-operators\/","title":{"rendered":"C programming Smallest of three integers without comparison operators"},"content":{"rendered":"<p>Write a C program to find the smallest of three integers, without using any of the comparison operators.<\/p>\n<p>Let 3 input numbers be x, y and z.<\/p>\n<p><strong>Method 1 (Repeated Subtraction)<\/strong><br \/>\nTake a counter variable c and initialize it with 0. In a loop, repeatedly subtract x, y and z by 1 and increment c. The number which becomes 0 first is the smallest. After the loop terminates, c will hold the minimum of 3.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/> <br\/>int smallest(int x, int y, int z)<br\/>{<br\/>  int c = 0;<br\/>  while ( x &amp;&amp; y &amp;&amp; z )<br\/>  {<br\/>      x--;  y--; z--; c++;<br\/>  }<br\/>  return c;<br\/>}<\/code><\/pre> <\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"c color1 bold\">int<\/code> <code class=\"c plain\">main()<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"c color1 bold\">int<\/code> <code class=\"c plain\">x = 12, y = 15, z = 5;<\/code><\/div>\n<div class=\"line number16 index15 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"c functions bold\">printf<\/code><code class=\"c plain\">(<\/code><code class=\"c string\">\"Minimum of 3 numbers is %d\"<\/code><code class=\"c plain\">, smallest(x, y, z));<\/code><\/div>\n<div class=\"line number17 index16 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"c keyword bold\">return<\/code> <code class=\"c plain\">0;<\/code><\/div>\n<div class=\"line number18 index17 alt1\"><code class=\"c plain\">}<\/code><\/div>\n<div class=\"line number18 index17 alt1\"><\/div>\n<div class=\"line number18 index17 alt1\">This methid doesn\u2019t work for negative numbers. Method 2 works for negative nnumbers also.<\/div>\n<div>[ad type=&#8221;banner&#8221;]<\/div>\n<div class=\"line number18 index17 alt1\">\n<p><strong>Method 2 (Use Bit Operations)<\/strong><br \/>\nUse method 2 of this post to find minimum of two numbers (We can\u2019t use Method 1 as Method 1 uses comparison operator). Once we have functionality to find minimum of 2 numbers, we can use this to find minimum of 3 numbers.<\/p>\n<\/div>\n<div class=\"line number18 index17 alt1\">\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ See mthod 2 of http:\/\/www.geeksforgeeks.org\/archives\/2643<br\/>#include&lt;stdio.h&gt;<br\/>#define CHAR_BIT 8<br\/> <br\/>\/*Function to find minimum of x and y*\/<br\/>int min(int x, int y)<br\/>{<br\/>  return  y + ((x - y) &amp; ((x - y) &gt;&gt;<br\/>            (sizeof(int) * CHAR_BIT - 1)));<br\/>}<br\/> <br\/>\/* Function to find minimum of 3 numbers x, y and z*\/<br\/>int smallest(int x, int y, int z)<br\/>{<br\/>    return min(x, min(y, z));<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>   int x = 12, y = 15, z = 5;<br\/>   printf(&quot;Minimum of 3 numbers is %d&quot;, smallest(x, y, z));<br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 3 (Use Division operator)<\/strong><br \/>\nWe can also use division operator to find minimum of two numbers. If value of (a\/b) is zero, then b is greater than a, else a is greater. Thanks to gopinath and Vignesh for suggesting this method.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ Using division operator to find minimum of three numbers<br\/>int smallest(int x, int y, int z)<br\/>{<br\/>    if (!(y\/x))  \/\/ Same as &quot;if (y &lt; x)&quot;<br\/>        return (!(y\/z))? y : z;<br\/>    return (!(x\/z))? x : z;<br\/>}<\/code><\/pre> <\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"c color1 bold\">int<\/code><code class=\"c plain\">main()<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c color1 bold\">int<\/code><code class=\"c plain\">x = 78, y = 88, z = 68;<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c functions bold\">printf<\/code><code class=\"c plain\">(<\/code><code class=\"c string\">\"Minimum of 3 numbers is %d\"<\/code><code class=\"c plain\">, smallest(x, y, z));<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c keyword bold\">return<\/code><code class=\"c plain\">0;<\/code><\/div>\n<div class=\"line number16 index15 alt1\"><code class=\"c plain\">}<\/code><\/div>\n<div>[ad type=&#8221;banner&#8221;]<\/div>\n<\/div>\n<div class=\"line number16 index15 alt1\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Smallest of three integers without comparison &#8211; Bit Algorithm &#8211; Take a counter variable c and initialize 0. In a loop, repeatedly subtract x, y and z by 1.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,1],"tags":[75676,75658,75681,75664,75665,75688,75682,75653,75675,75684,75654,75669,75656,75662,75657,75655,75680,75674,75667,75671,75651,74828,75652,75666,75686,75588,74064,75687,75678,75663,75661,75583,75590,75668,75660,72725,74093,75672,75670,75673,74059,73146,75659,75679,75685],"class_list":["post-25826","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-coding","tag-6-digit-smallest-number","tag-algorithm-and-flowchart","tag-algorithm-and-flowchart-examples","tag-algorithm-and-flowchart-in-c","tag-algorithm-flowchart","tag-algorithm-for-greatest-of-three-numbers","tag-c-program-to-find-average-of-3-numbers-using-function","tag-c-program-to-find-the-greatest-of-three-numbers","tag-c-program-using-ternary-operator","tag-comparing-3-numbers-in-java","tag-conditional-operator-in-c","tag-example-of-flowchart","tag-example-of-problem-with-algorithm-and-flowchart","tag-flow-chart-example","tag-flowchart-algorithm","tag-flowchart-and-algorithm","tag-flowchart-example-problems","tag-flowchart-examples","tag-flowchart-examples-programming","tag-flowchart-in-c","tag-how-to-write-pseudocode","tag-if-else-c-programming","tag-largest-4-digit-number","tag-largest-5-digit-number","tag-max-of-3-numbers","tag-numbers-divisible-by-4","tag-numbers-divisible-by-7","tag-pl-sql-program-to-find-greatest-of-three-numbers","tag-program-to-find-greatest-of-three-numbers-in-java","tag-pseudocode-and-flowchart-examples","tag-pseudocode-examples","tag-smallest-2-digit-number","tag-smallest-4-digit-number","tag-smallest-5-digit-number","tag-smallest-8-digit-number","tag-smallest-c-program","tag-smallest-even-number","tag-smallest-five-digit-number","tag-smallest-four-digit-number","tag-smallest-one-digit-number","tag-smallest-three-digit-number","tag-sorting-program-in-c","tag-ternary-operator-in-c","tag-write-75-as-a-product-of-prime-factors","tag-write-a-program-to-find-largest-of-three-numbers"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25826"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25826\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}