

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count numbers with same first and last digits in C++
<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’s understand with examples.</p><p><strong>Input</strong> − first=8 last=40</p><p><strong>Output</strong> − Count of numbers with same first and last digits − 5</p><p><strong>Explanation</strong> − Numbers between 8 and 40 with same first and last digit −</p><pre class="result notranslate">8, 9, 11, 22, 33</pre><p><strong>Input</strong> − first=100 last=200</p><p><strong>Output</strong> − Count of numbers with same first and last digits: 5</p><p><strong>Explanation</strong> − Numbers between 100 and 200 with same first and last digit −</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>=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="http://tpcg.io/z4bY7aos " rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">#include <bits/stdc++.h> using namespace std; //to find starting digit int getFirstDigit(int num){ while (num >= 10) { num = num/ 10; } return num; } int getCount(int fst,int lst){ int count=0; for(int i=fst;i<=lst;i++){ int fdigit=getFirstDigit(i); int ldigit=i%10; //to get last digit if(fdigit==ldigit) //if both are equal increment count { ++count; } } return count; } int main(){ int first = 10, last = 23; cout<<"Numbers with same first and last digits:"<<getCount(first, last); return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output −</p><pre class="result notranslate">Numbers with same first and last digits:2</pre>
- Related Questions & Answers
- Count substrings with same first and last characters in C++
- Count Numbers with Unique Digits in C++
- Display records where first and last name begins with the same letter in MySQL
- Absolute difference between the first X and last X Digits of N?
- C++ code to count number of lucky numbers with k digits
- Program to find a sublist where first and last values are same in Python
- Count even length binary sequences with same sum of first and second half bits in C++
- Numbers With Repeated Digits in C++
- Print numbers having first and last bits as the only set bits
- Maximum length of the sub-array whose first and last elements are same in C++
- First and Last Three Bits in C++
- Find last k digits in product of an array numbers in C++
- Count subarrays with same even and odd elements in C++
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Count characters with same neighbors in C++
Advertisements