0
//Is there any simple idea other than it?
static int rev(int n){
    if (n==0)return 0;
    else return n%10^10+rev(n/10)   
}

I want to reverse number with single variable and recursion.

2
  • stackoverflow.com/questions/20670444/… Commented Mar 16, 2017 at 13:03
  • I did see the same answer later on but i was asking reversing a number with single variable. Commented Mar 16, 2017 at 13:26

1 Answer 1

0
//Here i got the idea.
public static void main(String[] args) {
    // Let the number be 139.
    int n=139;
    System.out.println("reverse is "+rev(n));
}
static int rev(int n){
    if (n<10)return n;
    else return n%10*(int) Math.pow(10,(int)Math.log10(n))+rev(n/10);
        //Math class is used to return the number of digits and power.
}

This gives:: reverse is 931

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.