user2745043
user2745043

Reputation: 199

Recusion to print integer in reverse order

I am just learning recursion and I am to print an input array in reverse order (without using any String or Character methods).

For example 4295 would be displayed as 5924.

public static void method_c(int n) {
    if (n > 0) {
        System.out.println(n % 10);
        n /= 10;
    }

have this code but it only returns the 5, so I'm guessing its not going back through doing the recursion. I thought that it may be the placement of n /= 10, but that only changed the number that was returned.

How would I fix it to go back through to print the whole integer?

Upvotes: 1

Views: 2143

Answers (5)

Matthias Fischer
Matthias Fischer

Reputation: 553

As others have already pointed out: In order to make your method work, change your if to a while:

public static void method_c(int n) {
    while (n > 0) {
        System.out.println(n % 10);
        n /= 10;
    }
}

Judging from your description, there seems to be an important misconception here though: What you are doing is an iteration and NOT a recursion. For a quick glimpse at the differences between iteration and recursion, look, for example, here.

Upvotes: 2

Jsdodgers
Jsdodgers

Reputation: 5302

For recursion, you are forgetting that you have to call the method inside of itself except in a base case, so you would want:

public static void method_c(int n) {
    if (n != 0) {
        Boolean negative = false;    
        if (n<0) {
            n*=-1;
            negative = true;
        }        
        System.out.print(n % 10);
        method_c(n/10);
        if (negative) System.out.print("-");
    }
}

Calling method_c on n/10 except when n is 0 will make the function recursive.

Upvotes: 1

barwnikk
barwnikk

Reputation: 976

It's working with a minus number!!!

public static void main(String[] args) {
    for(int i=0; i<15; i++) {
        int number = (int) (System.nanoTime()%1000)-500; //random number
        System.out.println(number+" - "+method_c(number)); //printing
    }
}
public static int method_c(int number) {
    String output = number<0?"-":"";
    if(number<0)number=-number;
    while (number > 0) {
        output += number % 10 + "";
        number /= 10;
    }
    return Integer.parseInt(output);
}

Sample output:

73 - 37
120 - 21
-395 - -593
216 - 612
-78 - -87
... more

Upvotes: 1

Ștefan
Ștefan

Reputation: 758

If you change your if to a while would achieve the desired result but it would be iterative, not recursive. A recursive method would call itself, after checking that a base stopping condition is met. You would probably want to write something like:

public static void method_c(int n) {
    if (n > 0) {
        System.out.println(n % 10);
        method_c(n / 10);
    }
}

Upvotes: 0

codingenious
codingenious

Reputation: 8653

Basic of recursion is to call the same method again from inside and that is missing.

public static void method_c(int n) 
{
    if (n > 0)
    {
        System.out.print(n % 10);
        n /= 10;
        method_c(n);
    }
}

This should fix the problem.

Upvotes: 11

Related Questions