# How to read a number from highest place value to lowest?

So we all know it's easy to read a number from lowest place value to highest. A simple algorithm would be like :

while(n):
a=n%10;
n/=10;


but the question is how do you read it oppositely?

for eg : 12312. must result in me getting first 1 then 2 then 3 and so on.

I don't wanna form an array or reverse the number, unless there is a way to reverse the number with constant time-complexity.

Edit : I think the question is extremely vague so I'll try to explain what I need it for.

So i've been working on optimizing my code for an algorithm to convert binary to decimal.

My algorithm is : while(n) c+=(n%2)<<b++ , n/=10;

But then I figured I don't need the variable b, if i could somehow start from the front then an algorithm like c=2*c+n%2 would work.

I don't really wanna use a while loop to reverse a number as I think it would be inefficient than just having an extra variable.

An alternative that I thought of was having a float c then c=c/2+n%2 but the you'll need to know the number of digits of n. Which I could use logarithm for but I don't know that at that point if it is just better to use another variable. At its heart it is a time vs space complexity problem.

• Usually questions that say "please accomplish X but don't do it using method Y" are not useful because they don't specify why you have rejected Y. It is better to specify what your requirements are rather than specifying what solutions you don't want to accept. If you are not happy with using an array or reversing the number, I suggest you spend some time to identify what requirements they violate (e.g., do you have limits on space complexity?).
– D.W.
Dec 20 '20 at 19:45
• @D.W. yeah it's more so time and space complexity issue I'll edit my post to specify it in detail.
– Avi
Dec 21 '20 at 5:21