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? By Bipin Kumar Last updated : January 05, 2024

Problem statement

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?

Python program to check the given number has 0's as digits or not

# 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

The output of the above example is:

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.

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

The output of the above example is:

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.

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.