{"id":17679,"date":"2025-06-12T14:35:28","date_gmt":"2025-06-12T14:35:28","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=17679"},"modified":"2025-06-12T14:35:30","modified_gmt":"2025-06-12T14:35:30","slug":"write-a-method-digitsum-that-accepts-an-integer-parameter-and-returns-the-sum-of-its-digits-2","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/write-a-method-digitsum-that-accepts-an-integer-parameter-and-returns-the-sum-of-its-digits-2\/","title":{"rendered":"Write a method digitSum that accepts an integer parameter and returns the sum of its digits"},"content":{"rendered":"\n<p>Write a method digitSum that accepts an integer parameter and returns the sum of its digits<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>The correct answer and explanation is:<\/strong><\/mark><\/p>\n\n\n\n<p>Here&#8217;s the correct implementation of the <code>digitSum<\/code> method in Java:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int digitSum(int number) {\n    int sum = 0;\n    while (number &gt; 0) {\n        sum += number % 10;  \/\/ Add the last digit to sum\n        number \/= 10;        \/\/ Remove the last digit\n    }\n    return sum;\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation (300 words)<\/strong><\/h3>\n\n\n\n<p>The <code>digitSum<\/code> method calculates the sum of the digits of a non-negative integer. This is a common task in programming that helps reinforce understanding of loops, arithmetic operations, and number manipulation.<\/p>\n\n\n\n<p>The core idea is to repeatedly extract the last digit of the number using the <strong>modulus operator (<code>%<\/code>)<\/strong>, and then remove that digit using <strong>integer division (<code>\/<\/code>)<\/strong>.<\/p>\n\n\n\n<p>Let\u2019s break the logic down step by step:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>:<br>We begin by initializing a variable <code>sum<\/code> to 0. This variable will keep track of the running total of the digits.<\/li>\n\n\n\n<li><strong>Looping through the digits<\/strong>:<br>A <code>while<\/code> loop is used to process each digit. The condition <code>number > 0<\/code> ensures that we continue looping until all digits have been processed.<\/li>\n\n\n\n<li><strong>Extracting the last digit<\/strong>:<br>Inside the loop, <code>number % 10<\/code> extracts the last digit of the number. For example, if <code>number<\/code> is 29107, <code>29107 % 10<\/code> equals 7. This digit is then added to <code>sum<\/code>.<\/li>\n\n\n\n<li><strong>Removing the last digit<\/strong>:<br>After extracting and adding the last digit, we remove it by dividing <code>number<\/code> by 10 using integer division (<code>number \/= 10<\/code>). Integer division discards the remainder, so <code>29107 \/ 10<\/code> becomes 2910.<\/li>\n\n\n\n<li><strong>Repeat<\/strong>:<br>The loop repeats this process for each digit until <code>number<\/code> becomes 0.<\/li>\n\n\n\n<li><strong>Return the result<\/strong>:<br>Once the loop ends, <code>sum<\/code> contains the total of all the digits, and we return this value.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<p>For <code>digitSum(29107)<\/code>, the loop adds:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>7 (29107 % 10)<\/li>\n\n\n\n<li>0 (2910 % 10)<\/li>\n\n\n\n<li>1 (291 % 10)<\/li>\n\n\n\n<li>9 (29 % 10)<\/li>\n\n\n\n<li>2 (2 % 10)<br>Total = 2 + 9 + 1 + 0 + 7 = <strong>19<\/strong><\/li>\n<\/ul>\n\n\n\n<p>This method is efficient and works for any non-negative integer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a method digitSum that accepts an integer parameter and returns the sum of its digits The correct answer and explanation is: Here&#8217;s the correct implementation of the digitSum method in Java: Explanation (300 words) The digitSum method calculates the sum of the digits of a non-negative integer. This is a common task in programming [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17679","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/17679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=17679"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/17679\/revisions"}],"predecessor-version":[{"id":17680,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/17679\/revisions\/17680"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=17679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=17679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=17679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}