Tuesday, March 15, 2011

Reversing Numbers

This morning, about 40 minutes after I woke up, I was asked to write a method that prints the digits of a number one by one from right to left. So that if you input 1234, the output will be:

4
3
2
1

Now that's easy. All you have to do is divide the number by 10 four times printing the remainder each time. Your code should look something like this:

[sourcecode language="java"]public static void print( int num ) {
if( num > 0 ) {
System.out.println( num % 10 );
print( num / 10 );
}
}[/sourcecode]

After that, I as asked to write a method that outputs the digits from left to right. Now if you're like me, this could be a challenge without the morning shower. So it took me a bit of time to figure it out - but I did, 10 minutes later...

This algorithm is a bit trickier than the first one. What we have to do now, is reverse the number then use the same method we have above. ie turn 1234 into 4321 then call print(4321). So here's how we do it:

[sourcecode language="java"]public static void printForwards(int num){
if (num > 0){
int revNum = 0;
while(num > 0){
revNum = (revNum * 10) + (num % 10); // 0+4 then 40+3 then 430+2 then 4320+1
num /= 10;
}
print(revNum);
}
}[/sourcecode]
If you're not wanting to go the long way, you can use recursion just like in the first method:

[sourcecode language="java"]public static void print( int num ) {
if( num > 0 ) {
print( num / 10 );
System.out.println( num % 10 );
}
}[/sourcecode]

There you have it. If you ever end up using this to take over the world, I demand that you make me the governor of Antarctica.

4 comments:

  1. I don't know Java, but wouldn't it be easier to just convert the number 1234 to the string '1234' and print it that way? Then you could just reverse the string to print it backward.

    ReplyDelete
  2. That could definitely be a simpler way to do it and for many programmers, that would probably be the more intuitive way of approaching the problem. Being an evolving ape, that's what I did at first - before I discovered that I was expected not to.

    Of course, you can also create an array and fill it with the digits then print the entries of that array in reverse order. Another way of doing it is to create a Stack, push each digit into it then pop them one by one (Since stacks are LIFO).

    There are many different ways to do it, but I chose to share the mathematical approach because I found it more interesting.

    ReplyDelete
  3. In that case here's another version that does the same thing without creating an intermediate representation of the reversed number. Note this is in Python, but I'm sure you could adapt it.

    n = 1234
    while n > 0:
    print n % 10
    n /= 10
    4
    3
    2
    1

    ReplyDelete
  4. That absolutely works too! I should have added it as another possible solution from the beginning but I was trying to focus on the mathematical algorithm to reverse numbers as implied by the title.

    Thanks for contributing. I'll edit the post and include the recursion solution. :)

    ReplyDelete