{"id":25950,"date":"2017-10-25T21:56:05","date_gmt":"2017-10-25T16:26:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25950"},"modified":"2017-10-25T21:56:05","modified_gmt":"2017-10-25T16:26:05","slug":"multiply-given-integer-3-5","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/multiply-given-integer-3-5\/","title":{"rendered":"Multiply a given Integer with 3.5"},"content":{"rendered":"<p>Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, \/, *.<\/p>\n<p>Examples:<br \/>\nInput: 2<br \/>\nOutput: 7<\/p>\n<p>Input: 5<br \/>\nOutput: 17 (Ignore the digits after decimal point)<\/p>\n<p><strong>Solution:<\/strong><br \/>\n1. We can get x*3.5 by adding 2*x, x and x\/2. To calculate 2*x, left shift x by 1 and to calculate x\/2, right shift x by 2.<\/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 multiplyWith3Point5(int x)<br\/>{<br\/>  return (x&lt;&lt;1) + x + (x&gt;&gt;1);<br\/>}    <br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>  int x = 4; <br\/>  printf(&quot;%d&quot;, multiplyWith3Point5(x));<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>2. Another way of doing this could be (8*x \u2013 x)\/2 (See below code). Thanks to ajaym for suggesting this.<\/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\/>int multiplyWith3Point5(int x)<br\/>{<br\/>  return ((x&lt;&lt;3) - x)&gt;&gt;1;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Multiply a given Integer with 3.5 &#8211; Bit Algorithm &#8211; Given a integer x, write a function that multiplies x with 3.5 and returns the integer result.<\/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,83577],"tags":[75083,75079,75088,76298,75269,75263,76294,75276,76276,76275,76304,69955,75274,76302,76292,75278,76305,76296,76299,76297,76300,76306,76290,76277,76303,76285,76288,75426,76087,76289,76282,69935,76281,76291,76286,76278,75271,75086],"class_list":["post-25950","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-integer","tag-bitwise-operations-in-c","tag-bitwise-operator-in-c","tag-bitwise-operator-in-c-example-programs","tag-bitwise-operator-in-c-language-example-programs","tag-bitwise-operators","tag-bitwise-operators-in-c","tag-bitwise-operators-in-c-with-example-programs","tag-bitwise-operators-in-embedded-c","tag-bitwise-operators-in-java-with-examples","tag-bitwise-programs-in-c","tag-bitwise-shift-operators-in-java-with-examples","tag-c-language-operator","tag-c-language-shift-operator","tag-c-multiplication","tag-c-operator","tag-c-programming-bits","tag-c-programming-operators","tag-c-unary-operator","tag-flag-mask","tag-java-binary-operator","tag-java-bit-shift-example","tag-java-bitwise","tag-java-bitwise-and","tag-logical-operators-in-c","tag-logical-operators-in-c-language-with-examples","tag-logical-operators-in-c-programming","tag-logical-shift","tag-mathematical-operators-in-c","tag-modulus-operator-in-c","tag-operator-c-programming","tag-operator-in-c","tag-operators-in-c-language","tag-operators-in-c-programming","tag-precedence-of-operators-in-c","tag-unary-operator-in-c","tag-various-operators-in-c","tag-xor-0","tag-xor-operator-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25950","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=25950"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25950\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}