<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: key</title>
    <description>The latest articles on Forem by key (@key_3fa4afa7c85990c86ea9a).</description>
    <link>https://forem.com/key_3fa4afa7c85990c86ea9a</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3422959%2Fb0f44888-1d25-4ec2-bfb7-9998b402979e.jpg</url>
      <title>Forem: key</title>
      <link>https://forem.com/key_3fa4afa7c85990c86ea9a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/key_3fa4afa7c85990c86ea9a"/>
    <language>en</language>
    <item>
      <title>How to calculate digit sum by C++</title>
      <dc:creator>key</dc:creator>
      <pubDate>Sat, 21 Feb 2026 11:35:39 +0000</pubDate>
      <link>https://forem.com/key_3fa4afa7c85990c86ea9a/how-to-calculate-digit-sum-272h</link>
      <guid>https://forem.com/key_3fa4afa7c85990c86ea9a/how-to-calculate-digit-sum-272h</guid>
      <description>&lt;h3&gt;
  
  
  Basic Approach
&lt;/h3&gt;

&lt;p&gt;Let's take &lt;strong&gt;2026&lt;/strong&gt; as an example.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2026 can be written as 2020 + 6.&lt;/li&gt;
&lt;li&gt;Since 2020 is divisible by 10, dividing 2026 by 10 leaves a remainder of 6.&lt;/li&gt;
&lt;li&gt;Using this property, we can extract the digit in the ones place.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;Here is the code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

int main(){
  int digit = 2026;

  // digit = 2026 (2020 + 6)
  cout &amp;lt;&amp;lt; "ones place: " &amp;lt;&amp;lt; digit % 10 &amp;lt;&amp;lt; endl;

  digit /= 10; // digit = 202 (200 + 2)
  cout &amp;lt;&amp;lt; "tens place: " &amp;lt;&amp;lt; digit % 10 &amp;lt;&amp;lt; endl;

  digit /=10; // digit = 20 (20 + 0)
  cout &amp;lt;&amp;lt; "hundreds place: " &amp;lt;&amp;lt; digit % 10 &amp;lt;&amp;lt; endl;

  digit /=10; // digit = 2
  cout &amp;lt;&amp;lt; "thousands place: " &amp;lt;&amp;lt; digit % 10 &amp;lt;&amp;lt; endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ones place: 6
tens place: 2
hundreds place: 0
thousands place: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Streamlined Implementation
&lt;/h3&gt;

&lt;p&gt;We can simplify this step using a while loop. Since each division by 10 removes the last digit, the number will eventually become 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

int main(){
  int digit = 2026;

  while (digit &amp;gt; 0) {
    cout &amp;lt;&amp;lt; digit % 10 &amp;lt;&amp;lt; endl;
    digit /= 10;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6
2
0
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>cpp</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
