{"id":1992,"date":"2016-07-01T00:01:29","date_gmt":"2016-07-01T07:01:29","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1992"},"modified":"2016-07-08T07:58:08","modified_gmt":"2016-07-08T14:58:08","slug":"tally-the-digits","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1992","title":{"rendered":"Tally the Digits"},"content":{"rendered":"<p>The programming adventure is full of interesting twists and turns, and with each new puzzle comes multiple opportunities to flex your coding skills and devise new solutions. For this month&#8217;s Exercise, the task is to take an integer value and add up all its digits. The mathematical term for this operation is a <em>digit sum<\/em>.<br \/>\n<!--more--><br \/>\nYou&#8217;ve probably performed this type of exercise in the past. For example, suppose you have the number 135. When you add all the digits you get 9:<\/p>\n<p><code>135 = 1 + 3 + 5 = 9<\/code><\/p>\n<p>And if the sum is more than a single digit, you repeat the process:<\/p>\n<p><code>246 = 2 + 4 + 6 = 12 = 1 + 2 = 3<\/code><\/p>\n<p>The digit sum procedure is recursive, meaning that when the result is more than a single digit, you start over again until only a single digit remains.<\/p>\n<p>I&#8217;m sure a mathematician can philosophize on the relevance of a digit sum, but that&#8217;s not the point of this month&#8217;s Exercise. You&#8217;re job is to come up with a function that returns the digit sum of an integer value. Here is a skeleton provided to help get you started:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint digit_sum(unsigned n)\r\n{\r\n}\r\n\r\nint main()\r\n{\r\n    unsigned v;\r\n\r\n    while(1)\r\n    {\r\n        printf(\"Type a positive number, 0 to quit: \");\r\n        scanf(\"%u\",&v);\r\n        if( v == 0)\r\n            break;\r\n        printf(\"The sum of the digits is %d\\n\",\r\n                digit_sum(v));\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is a sample run of how the program might work:<\/p>\n<pre><code>Type a positive number, 0 to quit: 123\r\nThe sum of the digits is 6\r\nType a positive number, 0 to quit: 246\r\nThe sum of the digits is 3\r\nType a positive number, 0 to quit: 20160701\r\nThe sum of the digits is 8\r\nType a positive number, 0 to quit: 0<\/code><\/pre>\n<p>I&#8217;ve provided the input loop for you. You need to code the <em>digit_sum()<\/em> function for the sample code. It receives an <em>unsigned int<\/em> value, <code>n<\/code>, which is magically manipulated to return a single digit value.<\/p>\n<p>I know of at least two ways to accomplish this task. You can <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1997\">click here<\/a> to view my solutions, but please try the Exercise on your own before peeking at what I did.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calculate the total value of digits in an integer. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1992\">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":[3],"tags":[],"class_list":["post-1992","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1992","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=1992"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1992\/revisions"}],"predecessor-version":[{"id":2018,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1992\/revisions\/2018"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}