text: Count numbers with unit digit k in given range | Efficient CPP program to count numbers with last digit as k in given range . ; Returns count of numbers with k as last digit . ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int countLastDigitK ( long long low , long long high , long long K ) { long long mlow = 10 * ceil ( low / 10.0 ) ; long long mhigh = 10 * floor ( high / 10.0 ) ; int count = ( mhigh - mlow ) / 10 ; if ( high % 10 >= K ) count ++ ; if ( low % 10 <= K && ( low % 10 ) ) count ++ ; return count ; } int main ( ) { int low = 3 , high = 35 , k = 3 ; cout << countLastDigitK ( low , high , k ) ; return 0 ; }