Results 1 to 7 of 7

Thread: [Resolved] Recursion: Reverse an Integer

Threaded View

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Recursion: Reverse an Integer

    I need to write a recursive function to reverse the order of digits in an integer. This is a homework problem, so it has to be done using recursion (not a loop or anything). This is what I've come up with so far:

    Code:
    public int revDigits (int num) { 
      if (num < 10) {
          return num;
      } else {
          int s = num / 10;
          int d = num % 10;
    
          int i = revDigits(s);
    
          return (d * 10) + i; // incorrect
    
          // need to find out how many digits in d:
          //return (d * 10^(num digits in d)) + i);
      }
    }
    The problem that I've come across is commetted at the bottom. Any help with solving this would be appreciated. Thanks in advance.
    Last edited by The Hobo; Nov 21st, 2004 at 10:31 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width