Home » Python » Python programs

Find the number of integers from 1 to n which contains digits 0's in Python

Here, we will see the python program to count how many integers from 1 to N contain 0's as digits?
Submitted by Bipin Kumar, on November 21, 2019

This tutorial going to most interesting because we have seen integer like 10, 20, 30, 40, 50, ..., 100 etc from 1 to 100 and one things come to our mind that this will easily calculate then why we use Python program to solve the question? that's ok but think when the range is too large then it will be complicated. A number N will provided by the user and we will find how many numbers have zero as digits up to the given value N. So, Here we will see the simple approach in Python to solve it.

Before going to solve the above problem, we will see how to check the given number has 0's as digits or not?

Program:

# 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

RUN 1:
Enter the value of n: 39406
Zero is found in 39406

RUN 2:
Enter the value of n: 123456
Zero is not found in 123456

Here, we have seen how to check the given number has zero as digits or not in Python? Now, by using the above concepts we will solve the above problem in a simple way.

Program:

# 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

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

Run 2: 
Enter the value of n: 8348
2229 number has zero as digits up to 8348.

Run 3:
Enter the value of n: 9000
2349 number has zero as digits up to 9000.

Explanation:

Here, we have assumed that the value of n provided by the user is 8348 and variable c used to count the integers which contain zero as a digit and initially, it assigns to zero. In the third line we are using for loop from 1 to n range in which we have to check integers and by using the in function we have done it. If it has zero as a digit then the value of c incremented by 1. So Guy's, I hope you have understood this tutorial.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.