I am stuck in a question in which lower bound L
and Upper bound U
is given.
Now suppose in the decimal representation of integer X
It seems you have the algorithm thought out already. Just break it down piece by piece and solve each part. I usually write something like you did there with comments and then break those down until they are at a reasonable bite size to write code for.
When you have it working, if needed, you can optimize it.