{"id":5926,"date":"2023-07-01T00:01:51","date_gmt":"2023-07-01T07:01:51","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5926"},"modified":"2023-08-19T09:10:11","modified_gmt":"2023-08-19T16:10:11","slug":"tally-the-digits-again","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5926","title":{"rendered":"Tally the Digits, Again"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>Seven years ago (to this same date!) I wrote <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1992\">an Exercise<\/a> to tally the digits in a number. The solution required that the sum be reduced to a single digit. For example, the sum of digits in 12345 is 15, which is then reduced to six.<br \/>\n<!--more--><br \/>\nUpon reflection, I wondered why I reduced the digits. After all, the sum of digits in the integer 12345 is 15. The result can be further reduced to six, but why? So I sought to re-write the solution using the string conversion method to tally the digits in an integer without reduction.<\/p>\n<p>My original &#8220;Tally the Digits&#8221; Exercise featured two solutions. The first uses math, the second converts the number into a string. Without peeking at my original string solution, I sought to rewrite it for this week&#8217;s Lesson. Then I compared the two.<\/p>\n<p>Here is my <em>digit_sum()<\/em> function from the Exercise&#8217;s string solution:<\/p>\n<pre class=\"screen\">\r\nint digit_sum(unsigned n)\r\n{\r\n    char digits[32];\r\n    char *d;\r\n    int total;\r\n\r\n    <span class=\"comments\">\/* convert value to string *\/<\/span>\r\n    snprintf(digits,32,\"%u\",n);\r\n\r\n    <span class=\"comments\">\/* initialize variables *\/<\/span>\r\n    d = digits;\r\n    total = 0;\r\n\r\n    <span class=\"comments\">\/* tally all the individual digits *\/<\/span>\r\n    while(*d)\r\n    {\r\n            <span class=\"comments\">\/* subtract '0' from the character to\r\n               retrieve its value *\/<\/span>\r\n        total += *d - '0';\r\n        d++;\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>For my update, I chose to use a <em>for<\/em> loop. This decision helped eliminate the initialization. Both variables used in the function, <code>x<\/code> and <code>t<\/code>, are initialized within the <em>for<\/em> loop statement.<\/p>\n<p>Variable <code>*d<\/code> isn&#8217;t necessary because in the <em>for<\/em> loop I use an offset to peer into the converted string.<\/p>\n<p>Finally, because I&#8217;m not reducing the value, the recursive call is removed from my <em>sum()<\/em> function. Here is my updated code with the new <em>sum()<\/em> function:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_07_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_07_01-Lesson.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    char num[32];\r\n    int x,t;\r\n\r\n    snprintf(num,32,\"%d\",a);\r\n    for( x=0, t=0; num[x]; x++ )\r\n        t+=num[x]-'0';\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>The <em>main()<\/em> function could be reduced by eliminating variable <code>t<\/code> and outputting the result of <code>sum(d)<\/code> directly in the <em>printf()<\/em> statement. But I prefer a more readable presentation.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter a positive integer: 12345<br \/>\nThe sum of the digits is 15<\/code><\/p>\n<p>The second way of tallying the digits in an integer is to use math. This solution is presented in the original Exercise, but it&#8217;s also <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5921\">this month&#8217;s Exercise<\/a> &mdash; again, without reducing the result.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calculating the sum of single digits in an integer, string method. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5926\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5926","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5926","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=5926"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5926\/revisions"}],"predecessor-version":[{"id":6011,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5926\/revisions\/6011"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}