Count of Numbers in Range where first digit is equal to last digit of the number | C ++ program to implement the above approach ; Base Case ; Calculating the last digit ; Calculating the first digit ; Drivers Code
#include <iostream> NEW_LINE using namespace std ; int solve ( int x ) { int ans = 0 , first , last , temp = x ; if ( x < 10 ) return x ; last = x % 10 ; while ( x ) { first = x % 10 ; x /= 10 ; } if ( first <= last ) ans = 9 + temp / 10 ; else ans = 8 + temp / 10 ; return ans ; } int main ( ) { int L = 2 , R = 60 ; cout << solve ( R ) - solve ( L - 1 ) << endl ; L = 1 , R = 1000 ; cout << solve ( R ) - solve ( L - 1 ) << endl ; return 0 ; }