Count the number of 2s as digit in all numbers from 0 to n.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains the input integer n.
Output:
Print the count of the number of 2s as digit in all numbers from 0 to n.
Constraints:
1<=T<=100
1<=N<=10^5
Example:
Input:
2
22
100
Output:
6
20

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Number theory

Starter code: #User function Template for python3



def number0f2s(n):

    

    #add Code here

    

def numberOf2sinRange(n):

    

    #add code here

Solutions:
def numberOf2sinRange(n):
	s = ''
	for i in range(n + 1):
		s += str(i)
	return s.count('2')


dic = {0: 0}

def dicSearch(n):
	maxk = -1
	for key in dic.keys():
		if maxk < key and key < n:
			maxk = key
	return maxk

def number0f2s(n):
	count = 0
	while n > 0:
		rem = n % 10
		if rem == 2:
			count += 1
		n //= 10
	return count

def numberOf2sinRange(n):
	mkey = dicSearch(n)
	count = dic[mkey]
	for i in range(mkey + 1, n + 1):
		count += number0f2s(i)
	dic[n] = count
	return count


def numberOf2sinRange(n):
	sum = 0
	for i in range(n + 1):
		x = str(i).count('2')
		sum += x
	return sum


def number0f2s(n):
	return str(n).count('2')

def numberOf2sinRange(n):
	c = 0
	for i in range(2, n + 1):
		c += number0f2s(i)
	return c


def numberOf2sinRange(n):
	i = 0
	x = ''
	for c in range(n + 1):
		x += str(c)
	for u in x:
		if u == '2':
			i += 1
	return i


def numberOf2sinRange(n):
	l = ''
	for i in range(n + 1):
		if '2' in str(i):
			l += str(i)
	return l.count('2')


def numberOf2sinRange(n):
	ret = 0
	for i in range(n + 1):
		s = str(i)
		c = s.count('2')
		ret += c
	return ret


def numberOf2sinRange(n):
	count = 0
	for i in range(1, n + 1):
		temp = str(i)
		for j in temp:
			if j == '2':
				count += 1
	return count


def numberOf2sinRange(n):
	if n <= 1:
		return 0
	if n < 11 and n > 1:
		return 1
	c = 0
	for i in range(1, n + 1):
		while i > 0:
			if i % 10 == 2:
				c += 1
			i //= 10
	return c


def number0f2s(n):
	count = 0
	while n > 0:
		i = n % 10
		if i == 2:
			count += 1
		n = n // 10
	return count

def numberOf2sinRange(n):
	res = 0
	for i in range(0, n + 1):
		res += number0f2s(i)
	return res


def number0f2s(n):
	a = ''
	for i in range(n + 1):
		a = a + str(i)
	return a.count('2')

def numberOf2sinRange(n):
	a = ''
	for i in range(n + 1):
		a = a + str(i)
	return a.count('2')


def number0f2s(n):
	ctr = 0
	while n:
		if n % 10 == 2:
			ctr += 1
		n = n // 10
	return ctr

def numberOf2sinRange(n):
	sum = 0
	for i in range(2, n + 1):
		sum += number0f2s(i)
	return sum


def number0f2s(n):
	pass

def numberOf2sinRange(n):
	f = ''
	for i in range(1, n + 1):
		f += str(i)
	return f.count('2')


def number0f2s(n):
	pass

def numberOf2sinRange(n):
	count = 0
	if n == 0:
		return 0
	if n < 9:
		return count + 1
	for i in range(0, n + 1):
		j = i
		while j != 0:
			temp = j % 10
			if temp == 2:
				count += 1
			j //= 10
	return count


arr = [0] * 100001
for i in range(2, 100001):
	arr[i] = arr[i - 1] + str(i).count('2')

def numberOf2sinRange(n):
	return arr[n]


def number0f2s(n):
	c = 0
	while n:
		r = n % 10
		if r == 2:
			c += 1
		n //= 10
	return c

def numberOf2sinRange(n):
	s = 0
	for i in range(2, n + 1):
		s += number0f2s(i)
	return s


def numberOf2sinRange(n):
	count = 0
	d = '2'
	l = [str(i) for i in range(0, n + 1)]
	for i in range(0, len(l)):
		if d in l[i]:
			j = 0
			while j < len(l[i]):
				if l[i][j] == d:
					count = count + 1
				else:
					pass
				j = j + 1
	return count


def number0f2s(n):
	count = 0
	for num in str(n):
		if num == '2':
			count += 1
	return count

def numberOf2sinRange(n):
	final = 0
	for i in range(n + 1):
		nums_2 = number0f2s(i)
		final += nums_2
	return final


def number0f2s(n):
	s = ''
	for i in range(n + 1):
		s += str(i)
	c = 0
	for a in s:
		if a == '2':
			c += 1
	return c

def numberOf2sinRange(n):
	return number0f2s(n)


from collections import Counter

def number0f2s(n):
	count = 0
	while n > 0:
		if n % 10 == 2:
			count += 1
		n //= 10
	return count

def numberOf2sinRange(n):
	res = 0
	for i in range(2, n + 1):
		res += number0f2s(i)
	return res


def number0f2s(n):
	s = str(n)
	return s.count('2')

def numberOf2sinRange(n):
	sum = 0
	for i in range(n + 1):
		sum += number0f2s(i)
	return sum


def numberOf2sinRange(n):
	res = 0
	for i in range(2, n + 1):
		res += str(i).count('2')
	return res


def number0f2s(n):
	return None

def numberOf2sinRange(n):
	x = ''
	for i in range(2, n + 1):
		x += str(i)
	return x.count('2')


def number0f2s(n):
	pass

def numberOf2sinRange(n):
	x = str(2)
	count = 0
	for i in range(n + 1):
		y = list(str(i))
		count += y.count(x)
	return count


def numberOf2sinRange(n):
	c = 0
	s = 0
	for i in range(n + 1):
		if '2' in str(i):
			s = str(i).count('2')
			c += s
	return c
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		print(numberOf2sinRange(n))


def numberOf2sinRange(n):
	cnt = 0
	for i in range(1, n + 1):
		while i > 0:
			if i % 10 == 2:
				cnt += 1
			i = i // 10
	return cnt
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		print(numberOf2sinRange(n))


def number0f2s(n):
	pass

def numberOf2sinRange(n):
	count = 0
	for i in range(n + 1):
		if '2' in str(i):
			count += str(i).count('2')
	return count
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		print(numberOf2sinRange(n))


def number0f2s(n):
	x = []
	for i in range(n + 1):
		x.append(str(i))
	return ''.join(x)

def numberOf2sinRange(n):
	y = 0
	for i in number0f2s(n):
		if i == '2':
			y += 1
	return y
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		print(numberOf2sinRange(n))


def number0f2s(n):
	i = 2
	d = 0
	while i <= n:
		s = str(i)
		d += s.count('2')
		i += 1
	return d

def numberOf2sinRange(n):
	i = 2
	d = 0
	while i <= n:
		s = str(i)
		d += s.count('2')
		i += 1
	return d


def twos(n):
	count = 0
	while n > 0:
		rem = n % 10
		if rem == 2:
			count = count + 1
		n = n // 10
	return count

def numberOf2sinRange(n):
	sum = 0
	for i in range(2, n + 1):
		sum = sum + twos(i)
	return sum


def number0f2s(n):
	c = 0
	while n != 0:
		if n % 10 == 2:
			c += 1
		n = n // 10
	return c

def numberOf2sinRange(n):
	v = 0
	for i in range(1, n + 1):
		v = v + number0f2s(i)
	return v


def number0f2s(n):
	n = str(n)
	count = 0
	for i in n:
		if i == '2':
			count = count + 1
	return count

def numberOf2sinRange(n):
	i = 1
	t = 0
	while i <= n:
		t = t + number0f2s(i)
		i = i + 1
	return t


def number0f2s(n):
	str_n = str(n)
	return len([x for x in str_n if x == '2'])

def rec_num_2s(i, j):
	if j < i:
		return 0
	if i == j:
		return number0f2s(i)
	mid = (i + j) // 2
	return rec_num_2s(i, mid) + rec_num_2s(mid + 1, j)

def numberOf2sinRange(n):
	res = rec_num_2s(0, n)
	return res


def number0f2s(n):
	str_n = str(n)
	return len([x for x in str_n if x == '2'])

def numberOf2sinRange(n):
	count = 0
	for x in range(n + 1):
		for char in str(x):
			if char == '2':
				count += 1
	return count


def number0f2s(n):
	c = 0
	while n != 0:
		x = n % 10
		n = n // 10
		if x == 2:
			c += 1
	return c

def numberOf2sinRange(n):
	ans = 0
	for i in range(1, n + 1):
		ans += number0f2s(i)
	return ans


def number0f2s(n):
	n = str(n)
	count = 0
	for i in range(len(n)):
		if n[i] == '2':
			count += 1
	return count

def numberOf2sinRange(n):
	ans = 0
	for i in range(2, n + 1):
		ans += number0f2s(i)
	return ans


def number0f2s(x, r):
	if r == 1 or r == 0:
		c = 0
	elif 2 <= r <= 10:
		c = 1
	elif 2 <= r <= 19:
		c = 2
	elif 20 <= r <= 29:
		c = r // 10 + x % 10 + 1
	elif 1 <= r <= 100:
		c = r // 10 + 10
	elif 200 <= r <= 299:
		c = 20 * (r // 100) + x % 100 + 1
	elif 100 <= r <= 1000:
		c = 20 * (r // 100) + 100
	elif 2000 <= r <= 2999:
		c = 300 * (r // 1000) + x % 1000 + 1
	elif 1000 <= r <= 10000:
		c = 300 * (r // 1000) + 1000
	elif 20000 <= x <= 29999:
		c = 4000 * (20000 // 10000) + x % 10000 + 1
	elif 10000 <= r <= 100000:
		c = 4000 * (r // 10000) + 10000
	return c

def numberOf2sinRange(n):
	count = 0
	a = 10
	x = n
	while n:
		r = n % a
		n = n - r
		count = count + number0f2s(x, r)
		a = a * 10
	return count


def numberOf2sinRange(n):
	l = []
	for i in range(n + 1):
		l.append(i)
	l = str(l)
	a = ''.join(l)
	return a.count('2')


def number0f2s(n):
	pass

def numberOf2sinRange(n):
	ans = 0
	for i in range(n + 1):
		idk = str(i)
		ans += idk.count('2')
	return ans


def number0f2s(n):
	total = 0
	while n > 0:
		if n % 10 == 2:
			total += 1
		n //= 10
	return total

def numberOf2sinRange(n):
	total = 0
	for num in range(1, n + 1):
		total += number0f2s(num)
	return total