Sunday, February 27, 2011

Reversing integers

I think I found a better way of reversing integers with a little Google help. I'll have to make sure I understand it on paper before putting it to code.

Basically it's this:

A number 12345 % 10 is 5. If you stay in int-land then 12345 / 10 is 1234. So, keep asking for the number % 10 and then divide by 10 and repeat until your number is 0. That solves the problem of getting the numbers in reverse order, but how do I store that?

Hm. Well, it's something. Further investigation is necessary.

EDIT: I suppose I could keep adding the numbers from the mod statement to a variable and just multiply them by 1, 10, 100, etc.. (10 ^ loop iteration).

(1*5) + (10*4) + (100*3) + (1000*2) + (10000*1)

No comments:

Post a Comment