Q:

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

belongs to collection: Python basic programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Check whether a number is a power of another numbe... >>
<< Find the day of the week for a given date in the p...