{"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=\u201dbanner\u201d]\n<p>Following is C implementation of the above idea.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20replace%20all%20%E2%80%980%E2%80%99%20with%20%E2%80%985%E2%80%99%20in%20an%20input%20Integer%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20recursive%20function%20to%20replace%20all%200s%20with%205s%20in%20an%20input%20number%0A%2F%2F%20It%20doesn\u2019t%20work%20if%20input%20number%20itself%20is%200.%0Aint%20convert0To5Rec(int%20num)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%20for%20recursion%20termination%0A%20%20%20%20if%20(num%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Extraxt%20the%20last%20digit%20and%20change%20it%20if%20needed%0A%20%20%20%20int%20digit%20%3D%20num%20%25%2010%3B%0A%20%20%20%20if%20(digit%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20digit%20%3D%205%3B%0A%20%0A%20%20%20%20%2F%2F%20Convert%20remaining%20digits%20and%20append%20the%20last%20digit%0A%20%20%20%20return%20convert0To5Rec(num%2F10)%20*%2010%20%2B%20digit%3B%0A%7D%0A%20%0A%2F%2F%20It%20handles%200%20and%20calls%20convert0To5Rec()%20for%20other%20numbers%0Aint%20convert0To5(int%20num)%0A%7B%0A%20%20%20%20if%20(num%20%3D%3D%200)%0A%20%20%20%20%20%20%20return%205%3B%0A%20%20%20%20else%20return%20%20convert0To5Rec(num)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20num%20%3D%2010120%3B%0A%20%20%20%20printf(%22%25d%22%2C%20convert0To5(num))%3B%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201dC Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>15125<\/pre>\n[ad type=\u201dbanner\u201d]\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}]}}