{"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[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%20%0Aint%20smallest(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20int%20c%20%3D%200%3B%0A%20%20while%20(%20x%20%26%26%20y%20%26%26%20z%20)%0A%20%20%7B%0A%20%20%20%20%20%20x\u2013%3B%20%20y\u2013%3B%20z\u2013%3B%20c%2B%2B%3B%0A%20%20%7D%0A%20%20return%20c%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]<\/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[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20See%20mthod%202%20of%20http%3A%2F%2Fwww.geeksforgeeks.org%2Farchives%2F2643%0A%23include%3Cstdio.h%3E%0A%23define%20CHAR_BIT%208%0A%20%0A%2F*Function%20to%20find%20minimum%20of%20x%20and%20y*%2F%0Aint%20min(int%20x%2C%20int%20y)%0A%7B%0A%20%20return%20%20y%20%2B%20((x%20-%20y)%20%26%20((x%20-%20y)%20%3E%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20(sizeof(int)%20*%20CHAR_BIT%20-%201)))%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20find%20minimum%20of%203%20numbers%20x%2C%20y%20and%20z*%2F%0Aint%20smallest(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20%20%20return%20min(x%2C%20min(y%2C%20z))%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20int%20x%20%3D%2012%2C%20y%20%3D%2015%2C%20z%20%3D%205%3B%0A%20%20%20printf(%22Minimum%20of%203%20numbers%20is%20%25d%22%2C%20smallest(x%2C%20y%2C%20z))%3B%0A%20%20%20return%200%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20Using%20division%20operator%20to%20find%20minimum%20of%20three%20numbers%0Aint%20smallest(int%20x%2C%20int%20y%2C%20int%20z)%0A%7B%0A%20%20%20%20if%20(!(y%2Fx))%20%20%2F%2F%20Same%20as%20%22if%20(y%20%3C%20x)%22%0A%20%20%20%20%20%20%20%20return%20(!(y%2Fz))%3F%20y%20%3A%20z%3B%0A%20%20%20%20return%20(!(x%2Fz))%3F%20x%20%3A%20z%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]<\/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}]}}