Subj : Re: Reversing a number To : comp.programming From : Irrwahn Grausewitz Date : Mon Sep 12 2005 01:45 pm "Rob Thorpe" wrote: >Willem wrote: >> Roger wrote: >> ) "Irrwahn Grausewitz" wrote in message >> ) news:shv7i1p3on0qpsr2rumeifst69o8tjtp32@4ax.com... >> )> int n = 0; >> )> >> )>> while (i) >> )>> { >> )>> n *= 10; >> )>> n += i % 10; >> )>> i /= 10; >> )>> } >> )>> return n; >> )>>} >> ) >> ) Then reverse(10000) is (1). Correct. After all, the reverse of 10000 _is_ 1. The reverse of "10000" would be "00001", but that's reversing a string, not a number. >> Of course it is; the function returns an integer, after all. > >I think the OP means a number in the mathematical sense of the word. >ie:- > >1 -> 1 >21 -> 12 >5634 -> 4365 .... which is exactly what above code does. Look Ma, no strings. >As Jerry Quinn pointed out it's simplest to convert it to a string then >reverse it and convert it back again. Is it? 10000 --> "10000" --> "00001" --> 1 Great improvement. :> Best regards -- Irrwahn (irrwahn35@freenet.de) .