'reversing an integer in java without a loop

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.



Solution 1:[1]

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);
}

Solution 2:[2]

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!

Solution 3:[3]

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

Solution 4:[4]

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);

Solution 5:[5]

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;
    }
    
    
}

Solution 6:[6]

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!");
}

}

Solution 7:[7]

int reverse = 0;

Scanner input = new Scanner(System.in);
int number = input.nextInt();

while(number !=0){
reverse = reverse * 10 + number % 10;
number = number /10;
}
System.out.println(reverse);

I have made a you tube video on this with great explanation. Check out the video if you are confused. https://www.youtube.com/watch?v=W7z0Yi02xpc&t=353s

Solution 8:[8]

This will be fast.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Altaf Ansari
Solution 2
Solution 3 forpas
Solution 4 RanaUmer
Solution 5 narcis dpr
Solution 6
Solution 7 Samanja Cartagena
Solution 8 fatih