C # code for above implementation
using System ; class GFG { static int numberOf2sinRange ( int n ) { string s = " " ; for ( int i = 0 ; i < n + 1 ; i ++ ) s += i + " " ; int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '2' ) { count ++ ; } } return count ; }