+ 2
is it possible to check if a digit exists in an integer without converting into string?
so I have an assignment where Im given numbers from 1-100 and I’m supposed to give an output of the number of given numbers containing the digit 5. The catch is that I can’t convert it to a string at all. Anyone knows how to do this?
2 Respostas
+ 4
You may try something like this
l = []
for i in range(101):
if i in range(50,60) or (i%5 == 0 and i%10 != 0):
l.append(i)
print(l)
+ 4
You can extract the last digit of a number, by taking the remainder after division by 10, which is expressed as
number % 10
Then you can move on to the next digit, from right to left, dividing the number by 10, and repeating the last digit check.