• 0

reversing integer numbers?


Question

I just came back from a MS interview and I was asked to reverse integer numbers. Needless to say I completely bombed the question. You can't use toString () methods and use the reverse string function call ( I tried to do that). So how do you reverse integer numbers mathematically?! Say I have an integer 123, how do I mathematically manipulate it so it comes out to be 321?

Link to comment
https://www.neowin.net/forum/topic/287002-reversing-integer-numbers/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Think of the (decimal) number 123 as 1 * 10 ^ 2 + 2 * 10 ^ 1 + 3 * 10 ^ 0, now this should map to 3 * 10 ^ 2 + 2 * 10 ^ 1 + 3 * 10 ^ 0.

In Haskell

reverseInt :: Int -> Int
reverseInt i  = let calculateInt n b
    | null n = 0
  	| otherwise = (head n) * 10  ^ (b) + calculateInt (tail n) (b+1) 
	in
	calculateInt (sepInt i) 0 where
  sepInt i
   | i == 0  = []
   | otherwise = (sepInt (i `div` 10)) ++ [(i `mod` 10)]

  • 0
  Elagizy said:
Hey... Check this >>

Imports vb = Microsoft.VisualBasic

Text1.Text = vb.StrReverse("123") -------- > 321

585500504[/snapback]

  Quote
You can't use toString () methods and use the reverse string function call ( I tried to do that). So how do you reverse integer numbers mathematically?!

I never even thought of the string reverse method. That would've been slick.

Here's what I came up with.

private int Reverse(int value)
  {
  	int reverse = 0;
  	while(value > 0)
  	{
    int part = value % 10;
    value /= 10;
    reverse *= 10;
    reverse += part;
  	}
  	return reverse;
  }

  • 0

for those of you who want this in vb, here it is

Private Shared Function Reverse(ByVal num As Integer) As Integer
	Dim value As Integer

	While num > 0
  Dim part As Integer = num Mod 10
  num \= 10   'must use integer division instead of normal division
  value *= 10
  value += part
	End While

	Return value
End Function

the only weird part is the use of backwards slash, which denotes integer division in vbnet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Hey Nick! 👋 No worries at all — you're not alone in this, and it's great you're asking for help. Let me walk you through a simple, step-by-step fix using the tool mentioned on that AOMEI Partition Assistant guide — it really does the trick for many people with this exact issue! ✅ Here’s how to restore your USB back to full 256GB on Windows 11: Download & Install AOMEI Partition Assistant Go to the link you shared: AOMEI Partition Assistant and click the Download Freeware button. Insert your USB stick (Sandisk Ultra 256GB) Make sure it's properly connected. Wait for the system to detect it. Launch AOMEI Partition Assistant Once open, you'll see all your connected drives listed. Locate your USB Drive Look for the one that says something like Disk X – Removable and shows only 3.1GB or so. Right-click on your USB Drive’s Partition Then select Delete Partition → confirm the deletion. Now, right-click on the unallocated space (it should now show full unallocated capacity) Choose Create Partition → format it as FAT32 or exFAT (recommended for larger drives) → click OK. Click “Apply” in the top left corner Then hit Proceed to execute the pending operations. Wait a bit... and boom! 🎉 Your USB should now be restored to its full 256GB capacity!
    • When Facebook bought Beat Saber, they promised exactly this would never happen. Facebook can’t not lie.
    • Depends on what you mean by "this data". Nvidia can show you quite a few of those as well in their performance overlay, and I guess they might've assumed that if anyone wants to see more, they'll just use Afterburner as the de facto standard. As for real framerate vs framegen framerate, I don't think they exactly want you to know, given that their marketing has been strongly focused on hiding the real framerate and pretending the generated one is all that matters...
    • Yeah, but that never works out well. You can just give your power user more options and they'll be happy. Your new generation can just go with whatever you think is the best default.
  • Recent Achievements

    • First Post
      Fuzz_c earned a badge
      First Post
    • First Post
      TIGOSS earned a badge
      First Post
    • Week One Done
      slackerzz earned a badge
      Week One Done
    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      713
    2. 2
      ATLien_0
      279
    3. 3
      Michael Scrip
      209
    4. 4
      +FloatingFatMan
      201
    5. 5
      Steven P.
      131
  • Tell a friend

    Love Neowin? Tell a friend!