<?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>DEV Community: Ayush Mishra</title>
    <description>The latest articles on DEV Community by Ayush Mishra (@ayush_mishra_be0655d6b0d6).</description>
    <link>https://dev.to/ayush_mishra_be0655d6b0d6</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%2F3558262%2Ffe651e06-dafa-46af-8b1d-b750762b19d1.png</url>
      <title>DEV Community: Ayush Mishra</title>
      <link>https://dev.to/ayush_mishra_be0655d6b0d6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayush_mishra_be0655d6b0d6"/>
    <language>en</language>
    <item>
      <title>Splitting a Number into Digits</title>
      <dc:creator>Ayush Mishra</dc:creator>
      <pubDate>Fri, 10 Oct 2025 22:52:08 +0000</pubDate>
      <link>https://dev.to/ayush_mishra_be0655d6b0d6/splitting-a-number-into-digits-3a4m</link>
      <guid>https://dev.to/ayush_mishra_be0655d6b0d6/splitting-a-number-into-digits-3a4m</guid>
      <description>&lt;p&gt;Understanding how to split a number into its individual digits is a fundamental skill in programming. This technique is particularly useful for solving problems like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/add-digits/" rel="noopener noreferrer"&gt;LeetCode 258: Add Digits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/count-the-digits-that-divide-a-number/description/" rel="noopener noreferrer"&gt;LeetCode 2520. Count the Digits That Divide a Number&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/compute-alternating-sum/" rel="noopener noreferrer"&gt;LeetCode 3701: Compute Alternating Sum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/alternating-digit-sum/" rel="noopener noreferrer"&gt;LeetCode 2544: Alternating Digit Sum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/separate-the-digits-in-an-array/" rel="noopener noreferrer"&gt;LeetCode 2553: Separate the Digits in an Array&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/" rel="noopener noreferrer"&gt;LeetCode 2535: Difference Between Element Sum and Digit Sum of an Array&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By mastering the methods described below, you'll be good to tackle these and similar problems.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. From First Digit to Last Digit
&lt;/h3&gt;

&lt;p&gt;To extract digits from left to right, we need to isolate the first digit and then work our way down. This is where &lt;code&gt;log10&lt;/code&gt; and powers of 10 come in.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Formula
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;d = pow(10, floor(log10(n)))
first_digit = floor(n / d)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;log10(n)&lt;/code&gt; tells you the power to which 10 must be raised to get &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;n = 512&lt;/code&gt; → &lt;code&gt;log10(512) ≈ 2.7&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This means &lt;code&gt;10^2.7 ≈ 512&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;floor(log10(n))&lt;/code&gt; removes the decimal, leaving just the exponent of the largest power of 10 smaller than &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;floor(2.7) = 2&lt;/code&gt; → the largest power of 10 smaller than 512 is &lt;code&gt;10^2 = 100&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;pow(10, k)&lt;/code&gt; gives the divisor to scale the number down to a value between 1 and 10.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;512 / 100 = 5.12&lt;/code&gt; → take the floor → &lt;code&gt;5&lt;/code&gt;, the first digit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After getting the first digit, reduce &lt;code&gt;n&lt;/code&gt; using modulo: &lt;code&gt;n % d = 512 % 100 = 12&lt;/code&gt; → then repeat with the remaining number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n = 512
d = 10^2 = 100
First digit: floor(512 / 100) = 5
Remaining: 512 % 100 = 12

Next digit: floor(12 / 10) = 1
Remaining: 12 % 10 = 2

Last digit: 2

// 5,1,2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. From Last Digit to First Digit
&lt;/h3&gt;

&lt;p&gt;Simpler because modulo handles it directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n = 512
512 % 10 = 2
512 // 10 = 51

51 % 10 = 1
51 // 10 = 5

5 % 10 = 5
5 // 10 = 0 → done

/*
Result : 2,1,5
*/ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the same order. This involves splitting each number into its digits and combining them.&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>algorithms</category>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
