{"id":5921,"date":"2023-07-01T00:01:19","date_gmt":"2023-07-01T07:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5921"},"modified":"2023-07-08T10:56:01","modified_gmt":"2023-07-08T17:56:01","slug":"the-sum-of-the-integers-digit-is","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5921","title":{"rendered":"The Sum of the Integer&#8217;s Digit Is . . ."},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>Yes, this Exercise has been <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1992\">done before<\/a>. But back then, exactly seven years ago, I had the digits reduced further so that the result was a single digit. This time, your goal is to tally the digits in an integer and report the results &mdash; no reductions!<br \/>\n<!--more--><br \/>\nTo tally digits in an integer you take a value such as 1155448. This integer contains digits that total to 28. Your solution to this Exercise consumes a value such as 1155448 and outputs the sum of those individual digits, which is equal to 28.<\/p>\n<p>The Exercise from seven years ago had you reduce the value further. So 28 becomes ten, which becomes one. That&#8217;s not the task at hand for this challenge. No, your job is <em>not<\/em> to reduce the tally.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5926\">Today&#8217;s Lesson<\/a> covers the string method of reducing digits. This approach is valid, but it&#8217;s not what I&#8217;m requesting for a solution. Instead, you must use frightening math to reduce the digits. Here&#8217;s a sample run:<\/p>\n<p><code>Enter a positive integer: 1155448<br \/>\nThe sum of the digits is 28<\/code><\/p>\n<p><strong>Do not cheat by looking up the old solution!<\/strong><\/p>\n<p>Feel free to borrow the <em>main()<\/em> function from today&#8217;s Lesson. Your task is to rewrite the <em>sum()<\/em> function so that it uses a <em>for<\/em> loop and math operations to reduce the digits. (The original solution, which you&#8217;re not to peek at, uses a <em>while<\/em> loop.)<\/p>\n<p>Please try this Exercise on your own before you check out my solution, or you can cheat and look at the original Exercise&#8217;s solution. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5939\">Click here<\/a> to view my solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write code that tallies the total of the digits in an integer without converting the integer into a string. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5921\">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-5921","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\/5921","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=5921"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5921\/revisions"}],"predecessor-version":[{"id":5954,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5921\/revisions\/5954"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}