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 . ; 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 ++ ) { string str = to_string ( i ) ; countr += count ( str . begin ( ) , str . end ( ) , '1' ) ; } return countr ; } int main ( ) { int n = 13 ; cout << countDigitOne ( n ) << endl ; n = 131 ; cout << countDigitOne ( n ) << endl ; n = 159 ; cout << countDigitOne ( n ) << endl ; return 0 ; }