Total no of 1 's in numbers | PHP code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code
< ? php function countDigitOne ( $ n ) { $ countr = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ str = strval ( $ i ) ; $ countr += substr_count ( $ str , '1' ) ; } return $ countr ; } $ n = 13 ; echo countDigitOne ( $ n ) . " STRNEWLINE " ; $ n = 131 ; echo countDigitOne ( $ n ) . " STRNEWLINE " ; $ n = 159 ; echo countDigitOne ( $ n ) . " STRNEWLINE " ; ? >