Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Darkor

Reversing a number

This topic is 5545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
How about this?


int Reverse( int Num )
{
int NewNum= 0;
while( Num )
{
NewNum= NewNum * 10 + Num % 10;
Num/= 10;
}
return NewNum;
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Ah thanks for the replies. Would never have thought of it myself really =p

Nope it ain''t from TopCoder, it''s from my friend''s lecture notes.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!