#### Archived

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

# 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.

## 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 on other sites

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

##### 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!

[edited by - stevenmarky on August 17, 2003 10:02:56 AM]

##### Share on other sites
That wouldn''t happen to be a TopCoder problem would it?

##### 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.

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632977
• Total Posts
3009676
• ### Who's Online (See full list)

There are no registered users currently online

×