Total no of 1 's in numbers | c ++ code to count the frequency of 1 in numbers less than or equal to the given number . ; function to count the frequency of 1. ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) { int divider = i * 10 ; countr += ( n / divider ) * i + min ( max ( n % divider - i + 1 , 0 ) , i ) ; } return countr ; } int main ( ) { int n = 13 ; cout << countDigitOne ( n ) << endl ; n = 113 ; cout << countDigitOne ( n ) << endl ; n = 205 ; cout << countDigitOne ( n ) << endl ; }