0

I'm trying to take an integer as a parameter and then use recursion to double each digit in the integer.

For example doubleDigit(3487) would return 33448877.

I'm stuck because I can't figure out how I would read each number in the digit I guess.

1
  • 3
    Not sure why you want to use recursion for that..? Commented Mar 8, 2011 at 0:23

5 Answers 5

1

To do this using recursion, use the modulus operator (%), dividing by 10 each time and accumulating your resulting string backwards, until you reach the base case (0), where there's nothing left to divide by. In the base case, you just return an empty string.

String doubleDigit(Integer digit) {

      if (digit == 0) {
        return "";
      } else {
        Integer thisDigit = digit % 10;
        Integer remainingDigits = (digit - thisDigit) / 10;
        return doubleDigit(remainingDigits) + thisDigit.toString() + thisDigit.toString();
      }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. This is exactly what i was looking for!!! thank you very much! I never usually ask for help with coding but this one had me stumped. Thank you very much!
1

If you're looking for a solution which returns an long instead of a String, you can use the following solution below (very similar to Chris', with the assumption of 0 as the base case):

long doubleDigit(long amt) {
    if (amt == 0) return 0;     
    return doubleDigit(amt / 10) * 100 + (amt % 10) * 10 + amt % 10;        
}

The function is of course limited by the maximum size of a long in Java.

Comments

1

I did the same question when doing Building Java Programs. Here is my solution which works for negative and positive numbers (and returns 0 for 0).

public static int doubleDigits(int n) {
    if (n == 0) {
        return 0;
    } else {
        int lastDigit = n % 10;
        return 100 * doubleDigits(n / 10) + 10 * lastDigit + lastDigit;
}

Comments

0

There's no need to use recursion here.

I'm no longer a java guy, but an approximation of the algorithm I might use is this (works in C#, should translate directly to java):

int number = 3487;
int output = 0;
int shift = 1;
while (number > 0) {
   int digit = number % 10;                 // get the least-significant digit       
   output += ((digit*10) + digit) * shift;  // double it, shift it, add it to output
   number /= 10;                            // move to the next digit
   shift *= 100;                            // increase the amount we shift by two digits
} 

This solution should work, but now that I've gone to the trouble of writing it, I realise that it is probably clearer to just convert the number to a string and manipulate that. Of course, that will be slower, but you almost certainly don't care about such a small speed difference :)

Edit: Ok, so you have to use recursion. You already accepted a perfectly fine answer, but here's mine :)

private static long DoubleDigit(long input) {       
    if (input == 0) return 0;                      // don't recurse forever!
    long digit = input % 10;                       // extract right-most digit
    long doubled = (digit * 10) + digit;           // "double" it
    long remaining = input / 10;                   // extract the other digits
    return doubled + 100*DoubleDigit(remaining);   // recurse to get the result
}

Note I switched to long so it works with a few more digits.

1 Comment

Unfortunately I had to use recursion besides that your code is perfect it does the trick. I was just tasked with using recursion on this one :/
0

You could get the String.valueOf(doubleDigit) representation of the given integer, then work with Commons StringUtils (easiest, in my opinion) to manipulate the String.

If you need to return another numeric value at that point (as opposed to the newly created/manipulated string) you can just do Integer.valueOf(yourString) or something like that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.