{"id":26235,"date":"2017-10-26T20:40:20","date_gmt":"2017-10-26T15:10:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26235"},"modified":"2017-10-26T20:40:20","modified_gmt":"2017-10-26T15:10:20","slug":"c-programming-replace-0-5-input-integer","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-replace-0-5-input-integer\/","title":{"rendered":"C Programming &#8211; Replace all \u20180\u2019 with \u20185\u2019 in an input Integer"},"content":{"rendered":"<p>Given a integer as a input and replace all the \u20180\u2019 with \u20185\u2019 in the integer.<br \/>\nExamples:<\/p>\n<pre>    102 - 152\r\n    1020 - 1525<\/pre>\n<p>Use of array to store all digits is not allowed.<span id=\"more-142566\"><\/span><\/p>\n<p>Source: <a href=\"http:\/\/www.geeksforgeeks.org\/amazon-interview-experience-set-136-sde-t\/\" target=\"_blank\" rel=\"noopener noreferrer\">Amazon interview Experience | Set 136 (For SDE-T)<\/a><\/p>\n<p><strong>We strongly recommend to minimize the browser and try this yourself first.<\/strong><br \/>\nThe idea is simple, we get the last digit using mod operator \u2018%\u2019. If the digit is 0, we replace it with 5, otherwise keep it as it is. Then we recur for remaining digits.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C implementation of the above idea.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/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\">\/\/ C program to replace all \u20180\u2019 with \u20185\u2019 in an input Integer<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A recursive function to replace all 0s with 5s in an input number<br\/>\/\/ It doesn&#039;t work if input number itself is 0.<br\/>int convert0To5Rec(int num)<br\/>{<br\/>    \/\/ Base case for recursion termination<br\/>    if (num == 0)<br\/>        return 0;<br\/> <br\/>    \/\/ Extraxt the last digit and change it if needed<br\/>    int digit = num % 10;<br\/>    if (digit == 0)<br\/>        digit = 5;<br\/> <br\/>    \/\/ Convert remaining digits and append the last digit<br\/>    return convert0To5Rec(num\/10) * 10 + digit;<br\/>}<br\/> <br\/>\/\/ It handles 0 and calls convert0To5Rec() for other numbers<br\/>int convert0To5(int num)<br\/>{<br\/>    if (num == 0)<br\/>       return 5;<br\/>    else return  convert0To5Rec(num);<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int num = 10120;<br\/>    printf(&quot;%d&quot;, convert0To5(num));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>15125<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Replace all \u20180\u2019 with \u20185\u2019 in an input Integer &#8211; Mathematical Algorithms &#8211; The idea is simple, we get the last digit using mod operator \u2018%\u2019.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[77980,77972,77993,70814,70812,70842,70868,70843,70815,73138,77988,77984,77995,70866,70870,75068,74832,77974,77459,77977,77986,73504,77983,77981,77979,77982,77989,77985,70983,77971,77975,77990,77997,73031,77973,77991,77978,77998,77992,77987,77441,77976,77996,73051,77999,73147,76507,76506,77994,73412],"class_list":["post-26235","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-1-2-print","tag-array-elements-are-stored-in","tag-array-flowchart-example","tag-array-in-c","tag-array-in-c-programming","tag-array-in-c-programming-examples","tag-array-in-c-programming-examples-with-output","tag-array-programming","tag-array-programs-in-c","tag-array-programs-in-c-with-output","tag-array-programs-in-java-with-output","tag-break-statement-java","tag-c-program-to-print-even-numbers-in-an-array","tag-c-programming-array-examples","tag-example-of-array-in-c","tag-function-program-in-c","tag-functions-in-c-programming-with-examples","tag-how-to-accept-input-from-user-in-python","tag-how-to-take-input-from-user-in-python","tag-input-10-numbers-using-array-in-java","tag-input-function-python","tag-java-array-programs","tag-java-for-loop-example","tag-java-program-using-for-loop","tag-java-while-loop","tag-java-while-loop-break","tag-looping-computer-programming","tag-looping-statements-in-java","tag-nested-for-loop-in-java","tag-nested-loops-java","tag-print-python","tag-python-3-raw_input","tag-python-end-program","tag-python-list","tag-python-print-example","tag-python-print-syntax","tag-python-prompt-for-input","tag-python-read-input","tag-python-read-line-from-stdin","tag-python-scan","tag-python-string-methods","tag-readprint","tag-simple-array-program-in-java","tag-string-python","tag-what-is-a-loop-in-programming","tag-write-a-complete-program-that-declares-an-integer-variable","tag-write-a-program-in-java-to-print-the-following-pattern","tag-write-a-program-to-print-the-given-pattern","tag-write-a-program-using-for-loop","tag-write-ac-program-to-insert-an-element-in-an-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26235","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26235"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26235\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}