Count numbers with same first and last digits in C++

C++Server Side ProgrammingProgramming

<p style="">We are given an interval [first, last]. The goal is to find the count of numbers that have the same first and last digit within this interval. For example, 232 has the same first and last digit as 2.</p><p>We will do this by traversing from i=first to i=last. For each number I compare its first digit with the last digit, if they are the same increment the count.</p><p>Let&rsquo;s understand with examples.</p><p><strong>Input</strong> &minus; first=8 last=40</p><p><strong>Output</strong> &minus; Count of numbers with same first and last digits &minus; 5</p><p><strong>Explanation</strong> &minus; Numbers between 8 and 40 with same first and last digit &minus;</p><pre class="result notranslate">8, 9, 11, 22, 33</pre><p><strong>Input</strong> &minus; first=100 last=200</p><p><strong>Output</strong> &minus; Count of numbers with same first and last digits: 5</p><p><strong>Explanation</strong> &minus; Numbers between 100 and 200 with same first and last digit &minus;</p><pre class="result notranslate">101, 111, 121, 131, 141, 151, 161, 171, 181, 191.</pre><h2>Approach used in the below program is as follows</h2><ul class="list"><li><p>We take two integers first and last to define range [first,last].</p></li><li><p>Function getFirstDigit(int num) takes a number and returns its first digit.</p></li><li><p>While num&gt;=10, divide num by 10. In the end num will have the first digit. Return this value.</p></li><li><p>Function getCount(int fst,int lst) takes range variables and returns the count of numbers with the same first and last digits.</p></li><li><p>Take the initial count as 0.</p></li><li><p>Using for loop start from i=fst to i=lst, for each i calculate it first digit by calling getFirstDigit(i) and store in fdigit. (fdigit=getFirstDigit(i)).</p></li><li><p>Calculate last digit as ldigit=i%10.</p></li><li><p>If ldigit==fdigit, means they are the same. Increment count.</p></li><li><p>Return count as result.</p></li></ul><h2>Example</h2><p><a class="demo" href=" " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;bits/stdc++.h&gt; using namespace std; //to find starting digit int getFirstDigit(int num){ &nbsp; &nbsp;while (num &gt;= 10) &nbsp; &nbsp; &nbsp; { num = num/ 10; } &nbsp; &nbsp;return num; } int getCount(int fst,int lst){ &nbsp; &nbsp;int count=0; &nbsp; &nbsp;for(int i=fst;i&lt;=lst;i++){ &nbsp; &nbsp; &nbsp; int fdigit=getFirstDigit(i); &nbsp; &nbsp; &nbsp; int ldigit=i%10; //to get last digit &nbsp; &nbsp; &nbsp; if(fdigit==ldigit) //if both are equal increment count &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ ++count; } &nbsp; &nbsp;} &nbsp; &nbsp;return count; } int main(){ &nbsp; &nbsp;int first = 10, last = 23; &nbsp; &nbsp;cout&lt;&lt;&quot;Numbers with same first and last digits:&quot;&lt;&lt;getCount(first, last); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Numbers with same first and last digits:2</pre>
Updated on 29-Aug-2020 11:57:28