Python Program to Count integers from 1 to N Contains 0’s and 1’s as Digits

python program to count integers from 1 to N contains 0’s and 1’s as digits; Through this tutorial, i am going to show you how to count integers from 1 to N contains 0’s and 1’s as digits in python program.

In this tutorial, i will write a python program to input an integers from 1 to N and check if it contains any zero 0 and one 1 in it.

Python Program to Find integers from 1 to N Contains 0’s and 1’s as Digits

  • Python Program Find the number of integers from 1 to n which contains digits 0’s
  • Python Program Count the number of integers from 1 to n which contains digits 0’s using For Loop
  • Python Program Count the number of integers from 1 to n which contains digits 0’s and 1’s using Function

Python Program Find the number of integers from 1 to n which contains digits 0’s

# Python Program to Find the number of integers from 1 to n which contains digits 0's 
# input the value of N
n=int(input('Enter the value of n: '))
s=str(n)
z=str(0)
if z in s:
    print('Zero is found in {}'.format(n))
else:
    print('Zero is not found in {}'.format(n))

Output

Enter the value of n:  10
Zero is found in 10

Python Program Count the number of integers from 1 to n which contains digits 0’s using For Loop

# Python Program to Find the number of integers from 1 to n which contains digits 0's 
# enter the value of N
n=int(input('Enter the value of n: '))
c=0
z=str(0)
for j in range(1,n+1):
    if z in str(j):
        c+=1 
print('{} number has zero as digits up to {}.'.format(c,n))

Output

Enter the value of n:  50
5 number has zero as digits up to 50.

Python Program Count the number of integers from 1 to n which contains digits 0’s and 1’s using Function

# Python Program to Find the number of integers 
# from 1 to n which contains 0's and 1's only 
n=int(input('Enter the value of n: '))
def countNumbers(x, n): 
	
	# If number is greater than n 
	if x > n : 
		return 0
	# otherwise add count this number and 
	# call two functions 
	return (1 + countNumbers(x * 10, n) +
				countNumbers(x * 10 + 1, n)) 
# Driver code 
if __name__ == '__main__': 
	print('The count is ', countNumbers(1, n)); 

Output

Enter the value of n:  200
The count is  7

Recommended Python Tutorials

Leave a Comment