Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Recursion help

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion help

    Hello, im new the forums and was wondering if you could help me with a problem I have to do for my Comp Sci class.

    The question is:

    Write a function called reverse that takes a long integer argument and returns the result of reversing its digits.

    for example System.out.print(reverse(-12); returns -21

    Im not just asking for someone to spew out the code but also if they had the time to also possibly explain it a little more for me?

    any help is appreciated thank you.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Recursion help

    Probably more than one way to do it (there always is), but I'll hint towards one way. First, think String. Second, think reverse the String.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Recursion help

    Here's some tips that hopefully should help you figure it out:

    Recursion is just breaking up a big problem into smaller pieces. What pieces can you break the problem up into (hint, see copeg's post)?

    Once you reach the smallest possible piece, you simply tell the program that there is a certain solution for that problem. The program will then "back-track" up and build up the final solution once it knows the solution of all the smaller problems.

    Ex:

    123456

    123456 reversed, it should end in 1. So, just need to reverse 23456 and put that in front of the 1.

    23456 reversed, it should end in 2. So, just need to reverse 3456 and put that in front of the 2.

    ...

    6 reversed is known (it is 6). Know the solution to this base case, now just put the final answer together.

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion help

    so this is what i came up with i think it will work.
    public static void Long(Long n){
    System.out.prinln(n % 10);
    if(n / 10 !=0)
    reverse(n/10);
     
    }

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Recursion help

    Remember, you have to do it in reverse:
    public static void Long(long n)
    {
        if (n / 10 != 0)
        {
              reverse(n/10);
         }
         System.out.println(n%10);
    }

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 09:53 PM
  3. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM