{"id":5939,"date":"2023-07-08T00:01:28","date_gmt":"2023-07-08T07:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5939"},"modified":"2023-06-30T21:13:18","modified_gmt":"2023-07-01T04:13:18","slug":"the-sum-of-the-integers-digit-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5939","title":{"rendered":"The Sum of the Integer&#8217;s Digit &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5921\">this month&#8217;s Exercise<\/a> is to tally the digits in an integer, reporting the sum. You must use math and not cheat by looking at the old solution, which I didn&#8217;t. Promise.<br \/>\n<!--more--><br \/>\nThough I&#8217;ve done this type of problem before, I devised what I believe to be a more clever solution than the first time.<\/p>\n<p>Central to the solution is to use the modulus operator to lop off digits from a number:<\/p>\n<p><code>a % 10<\/code><\/p>\n<p>The result of this operation is the last digit in a decimal integer. When <code>a<\/code> has the value 1235, the result is 5. When <code>a<\/code> is 4002, the result is 2.<\/p>\n<p>To obtain values for the rest of the digits, they must be shifted right one position. At the binary level, the C operator <code>&gt;&gt;<\/code> handles the task. For a decimal integer, however, you divide by ten:<\/p>\n<p><code>a = a \/ 10;<\/code><\/p>\n<p>When <code>a<\/code> is assigned the value 12345, the result of 12345\/10 is 1234. Because <code>a<\/code> is an <em>int<\/em> variable, the .5 portion of the value is discarded.<\/p>\n<p>Both of these expressions are put into a <em>for<\/em> loop, which tallies the digits in variable <code>a<\/code>, saving them in variable <code>t<\/code>:<\/p>\n<p><code>for( t=0; a; a\/=10 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;t+= a % 10;<\/code><\/p>\n<p>The <em>for<\/em> loop initializes variable <code>t<\/code> (the total) to zero. Variable <code>a<\/code> is passed to the <em>sum()<\/em> function, so it needs no initialization. But the value of variable <code>a<\/code> is used to terminate the loop. For each iteration, the value of variable <code>a<\/code> is divided by ten. The <em>for<\/em> loop&#8217;s statement uses the modulus operator to fetch the final digit in the number, adding it to the value of variable <code>t<\/code>.<\/p>\n<p>Here is my full solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_07-Exercise.c\" rel=\"noopener\" target=\"_blank\">2023_07-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint sum(int a)\r\n{\r\n    int t;\r\n\r\n    for( t=0; a; a\/=10 )\r\n        t+= a % 10;\r\n\r\n    return(t);\r\n}\r\n\r\nint main()\r\n{\r\n    int d,t;\r\n\r\n    <span class=\"comments\">\/* beg for input *\/<\/span>\r\n    printf(\"Enter a positive integer: \");\r\n    scanf(\"%d\",&amp;d);\r\n\r\n    <span class=\"comments\">\/* sum the digits *\/<\/span>\r\n    t = sum(d);\r\n    printf(\"The sum of the digits is %d\\n\",t);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>If you cheat and look at the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1997\">original Exercise&#8217;s solution<\/a>, you see this function:<\/p>\n<pre class=\"screen\">\r\nint digit_sum(unsigned n)\r\n{\r\n    int total = 0;\r\n\r\n    while(n)\r\n    {\r\n        total += n % 10;\r\n        n \/= 10;\r\n    }\r\n\r\n    <span class=\"comments\">\/* if the value is over two-digits wide, recurse *\/<\/span>\r\n    if(total > 9)\r\n        return(digit_sum(total));\r\n\r\n    return(total);\r\n}<\/pre>\n<p>The <em>while<\/em> loop approach is different and requires more overhead. I think my <em>for<\/em> loop solution is better, though both arrive at the same result. (Though remember that the original Exercise required further reduction of the digits, seen by the <code>if(total &gt; 9)<\/code> statement near the end of the function.)<\/p>\n<p>I hope your solution met with success. Remember, multiple ways exist to solve any programming puzzle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to tally the digits in an integer, reporting the sum. You must use math and not cheat by looking at the old solution, which I didn&#8217;t. Promise.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5939","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5939"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5939\/revisions"}],"predecessor-version":[{"id":5947,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5939\/revisions\/5947"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}