Reversing a number
I was pretty much humbled when faced with this question on how to rearrange the numbers in an integer such that the integer 234509 becomes 905432 using the modulus operator.
I you can convert it to a string and reverse it, but it -has- to use the modulus operator. And I''ll admit that I''m stumped...
Obviously this requires some clever mathematical trick that I haven''t come across. Mind if you could help?
Thanks and cheers!
How about this?
int Reverse( int Num ){ int NewNum= 0; while( Num ) { NewNum= NewNum * 10 + Num % 10; Num/= 10; } return NewNum;}
Well one way you could extract the seperate digits using modulus:
1) Perform modulus on the number first with 10, then minus the result of this off the original number.
2) Do step 1 again, but increase the number by tenfold each time (10, 100, 1000, 10000)
bleh too late!
p.s. Good answer
[edited by - stevenmarky on August 17, 2003 10:02:56 AM]
1) Perform modulus on the number first with 10, then minus the result of this off the original number.
2) Do step 1 again, but increase the number by tenfold each time (10, 100, 1000, 10000)
bleh too late!
p.s. Good answer
[edited by - stevenmarky on August 17, 2003 10:02:56 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement