Count all numbers up to N having M as the last digit | C ++ Program to implement the above approach ; Function to count the numbers ending with M ; Stores the count of numbers required ; Calculate count upto nearest power of 10 ; Computing the value of x ; Adding the count of numbers ending at M from x to N ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int N , int M ) { int total_count = 0 ; total_count += ( N / 10 ) ; int x = ( N / 10 ) * 10 ; if ( ( N - x ) >= M ) { total_count = total_count + 1 ; } return total_count ; } int main ( ) { int N = 100 , M = 1 ; cout << getCount ( N , M ) ; return 0 ; }