简体   繁体   中英

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.

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

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!

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

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

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

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

}

This will be fast.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM