3

This is an homework problem

Is there a way tor reverse a number in Java without using any loops? The only solution I can think of is reversing it using String and then casting it back to an integer.

3
  • Your question has the answer. What is it the you want? Commented Nov 18, 2018 at 5:00
  • 2
    Under the covers converting a number to a string, reversing a string and converting a string to a number all have loops under the covers. In fact, just about any sensible program to do this entails looping, one way or another. You need to tell us the requirements precisely as they were given to you. Commented Nov 18, 2018 at 6:36
  • .Homework problems expect you to demonstrate a technique so even if you could solve the problem another way, you have to solve it the expected way to get full marks. Commented Nov 18, 2018 at 12:55

7 Answers 7

3

If you want to reverse a number withour using any loop you can use Recursion method call. Following program is doing same

public static void reverseMethod(int number) {
    if (number < 10) {
        System.out.println(number);
        return;
    } else {
        System.out.print(number % 10);
        reverseMethod(number / 10);
    }
}

public static void main(String args[]) {
    int num = 4567;
    reverseMethod(num);
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if jvm does tail-recursion optimization and converts to loop?
1

Even if you were to reverse the number by casting it into a String, you would still need a loop if you want the program to work when having ints of different sizes. If I were to make a method to reverse a number but could not do it with loops, I would probably do it with recursion (which still uses loops indirectly). The code will look something like this:

class Main {
  public static void main(String[] args) {
    String input = "1234"; // or scanner to take in input can be implemented
    System.out.println(Integer.parseInt(reverseInt(input)));
  }
  public static String reverseInt(String x) {
    if (x.length() == 1) {
      return x;
    } else {
      return x.substring(x.length() - 1) + reverseInt(x.substring(0, x.length() - 1));
    }
   }


}

Hope this helps!

Comments

0

By using reverse() of StringBuilder:

int number = 1234;
String str = String.valueOf(number);
StringBuilder builder = new StringBuilder(str);
builder.reverse();
number = Integer.parseInt(builder.toString());
System.out.println(number);

will print:

4321

Comments

0

if you want reverse method without loop and recursion then use this code

 int a=12345;
    int b,c,d,e,f;
    b=a%10;
    c=a%100/10;
    d=a%1000/100;
    e=a%10000/1000;
    f=a%100000/10000;
    System.out.println(b+","+c+","+d+","+e+","+f);

Comments

0

you can go like :

public int reverse(int x) {    
    
    String o = "";
    
    if (x < 0) {
        x *= -1;
                String s = Integer.toString(x);

        o += "-";
        o += new StringBuilder(s).reverse().toString();
    }
    else {
                String s = Integer.toString(x);

        o += new StringBuilder(s).reverse().toString();
    }
    try {
        int out = Integer.parseInt(o);
    
    //System.out.println(s);
    
        return out;        }
    catch (NumberFormatException e) {
        return 0;
    }
    
    
}

Comments

0

This is a solution using recursive method call

public class Tester{

public static int findReverse(int num, int temp){
    if(num==0){
        return temp;
    }else if(num<10){
        return temp*10 + num; //up to this is stopping condition
    }else{
        temp = temp*10 + num%10;
        return findReverse(num/10, temp);
    }
}

public static void main(String args[]){
    int num = 120021;
    int reverseNum = findReverse(num, 0);
    System.out.println(reverseNum);
    if(num == reverseNum)
        System.out.println(num +" is a palindrome!");
    else
        System.out.println(num +" is not a palindrome!");
}

}

2 Comments

<This is a recursive method to find and return the reversed integer .please copy the code to an existing java class to run it
You can use static variable to hold the temp value if you want to make reverse() method to accept only one argument.
0

This will be fast.

static int reverseNum(int num) {
    StringBuilder sb = new StringBuilder(String.valueOf(num));
    sb.reverse();
    return Integer.parseInt(sb.toString());
}

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.