1

I have a question how to better tackle this task, I have a version, but I am sure there is a better and shorter way to do this maybe. I need to take any int number(return it as an int without turning it into a String), but never with a 0 at the end (100, 120) but like 1234, or 4132. I need to take this number and using recursion rewrite it the other way around example 1234 to 4321, 4132 to 2314, maybe there is a way this is called, i personally don't know about it.

Here is what I got:

public static int reverse(int r, int n, int k){
    if(r==0)
        return 0;
    else
        return + (r%10) * (int)Math.pow(10, (n-k-1))+reverse (r/10, n, k+1)
}

public static void main(String[] args) {
    System.out.println(reverse(1234, 4, 0));
}
Ryne Ignelzy
  • 137
  • 1
  • 2
  • 13
  • Questions asking for help improving working code are usually more appropriate on the [CodeReview](https://codereview.stackexchange.com/) stack exchange. – azurefrog Dec 22 '17 at 16:09
  • If your code ***works fine*** (that is mandatory condition) and you are looking for ways to improve it you should post this question at https://codereview.stackexchange.com/ instead of Stack Overflow. – Pshemo Dec 22 '17 at 16:09
  • Possible duplicate of [Generating all permutations of a given string](https://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string) – Bourbia Brahim Dec 22 '17 at 16:11
  • your code has another problems than recursive, reformat it – Gholamali Irani Dec 22 '17 at 16:13
  • Someone else with the same homework: https://stackoverflow.com/questions/9723912/reversing-a-string-with-recursion-in-java – Moob Dec 22 '17 at 16:25
  • just read the answers of [1](https://stackoverflow.com/questions/16392459/recursion-digits-in-reverse-order) [2](https://stackoverflow.com/questions/20670444/using-recursion-to-reverse-an-integer-without-the-use-of-strings) [3](https://stackoverflow.com/questions/6317446/which-of-recursion-method-is-better-and-why-for-reverse-digits-of-integer) – Gholamali Irani Dec 22 '17 at 18:51

5 Answers5

0

Working with a String representation of the int may make the code more readable.

Try:

Integer.parseInt(new StringBuilder(r+"").reverse().toString());
O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
0

Didn't notice the recursion part.

public static void main(String[] args) {
    int i = 589;
    System.out.println(reverse(i));
}
public static int reverse(int k){
    if(k/10 == 0){return k;}
    else return (k%10 * (int)Math.pow(10, (int)Math.log10(k))) + reverse(k /10);
}

Explanation:

  • k%10 gives you the last digit of an int
  • (int) (Math.log10(k)) returns number of Digits in an Integer minus one
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

Current code doesn't compile. Added a ) to this line:

from if(r==0{ change to if(r==0){

and added a ; in this line return + (r%10) * (int)Math.pow(10, (n-k-1))+reverse (r/10, n, k+1);

Your code after this two changes will look like:

public static int reverse(int r, int n, int k){
    if(r==0)
    {
        return 0;
    }else{
        return + (r%10) * (int)Math.pow(10, (n-k-1))+reverse (r/10, n, k+1);
    }
}

if the number ends with 0, the program will not show any special message to the user, i.e 1230 will return 321. In this case, maybe maybe print a message ("number must not end with a 0) or throw an exception?

yoav
  • 191
  • 1
  • 2
  • 10
0
    public static final int reverse(int number) {
         final int lastDigit = number % 10;
         final int length = (int) Math.log10(number);

         return (number < 10) ? number : (int) (Math.pow(10.0, length) * lastDigit + reverse(number / 10));
    }

If the number is lower then 10 it's the number itself. Otherwise it's the last digit multiplied with 10^n where n is the length of the number, so it's now at the position for the first digit.

Then add the result of a reverse of the rest number to it (the number without the last digit).

You take advance of the recursion function itself as it would already work to solve the big problem. You only have to think about the trivial end condition and one single step (which mostly is something you would suggest as the last step)

snap
  • 1,598
  • 1
  • 14
  • 21
0

This is the best way that I could make it using recursion and without conversions.

private static int myReverse(int n, int r) {
        if(n == 0)
            return r;

        int newR = r*10 + n%10;

        return myReverse(n/10, newR);
}

What I'm doing here is:

  • Two parameters: n - the number you want to reverse, r - the reversed number
  • The recursion stops when n equals 0 because it always dividing it by 10
  • newR - This variable is unnecessary but it´s better for 'understanding' porposes, first I multiply r by 10 so I can sum the last value o n. For example, reverse 123: along the way if r = 12 and n = 3, first 12*10 = 120, n%10 = 3 then r*10 + n%10 = 123

A 'pleasant' way with only one return statement:

private static int myReverse2(int n, int r) {
        return n == 0 ? r : myReverse2(n/10, r*10 + n%10);
}
Filipe Nóbrega
  • 564
  • 4
  • 18