Given two integer number n and d. The task is to find the number between 0 to n which contain the specific digit d.
Input : n = 20 d = 5 Output : 5 15 Input : n = 50 d = 2 Output : 2 12 20 21 22 23 24 25 26 27 28 29 32 42
Take a loop from 0 to n and check each number one by one, if the number contains digit d then print it otherwise increase the number. Continue this process until loop ended.
The number of values are 7 17 27 37 47
This approach uses every number as a String and checks digit is present or not. This approach use of String.indexOf() function to check if the character is present in the string or not.
String.indexOf() >= 0 means chaaracter is present
and String.indexOf() = -1 means character is not present
# Python 3 program to print the number
# which contain the digit d from 0 to n
def index(st, ch):
for i in range(len(st)):
if(st[i] == ch):
# function to display the values
def printNumbers(n, d):
# Converting d to character
st = “” + str(d)
ch = st
# Loop to check each digit one by one.
for i in range(0, n + 1, 1):
# initialize the string
st = “”
st = st + str(i)
# checking for digit
if (i == d or index(st, ch) >= 0):
print(i, end = ” “)
# Driver code
if __name__ == ‘__main__’:
n = 100
d = 5
# This code is contributed by
5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65 75 85 95
- Find nth number that contains the digit k or divisible by k.
- Program to find last digit of n'th Fibonnaci Number
- Find minimum possible digit sum after adding a number d
- Find last five digits of a given five digit number raised to power five
- Perform n steps to convert every digit of a number in the format [count][digit]
- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest number less than N with digit sum greater than the digit sum of N
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Queries on sum of odd number digit sums of all the factors of a number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Largest number less than N whose each digit is prime number
- Find the last digit of given series
- Find Last Digit of a^b for Large Numbers
- Find the occurrences of digit d in the range [0..n]
- Find the unit place digit of sum of N factorials
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.