0

I want to reverse an inputted number. I've written the code for it. But i need to know if it could have been done in any other much faster way. Please feel free to modify my code.

public static void main()throws IOException
{
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter a number");
    int num=Integer.parseInt(in.readLine());
    int no=num;
    int d;
    int rev_no=0;
    int digits=0;
    while(num>0)
    {
        num/=10;
        digits++;
    }
    while(no>0)
    {
        d=no%10;
        rev_no+=d*(Math.pow(10,(digits-1)));
        no/=10;
        digits--;
    }
    System.out.println(rev_no);
}
sanjay
  • 3
  • 3
  • 8
    Convert it to a string, [reverse it](http://stackoverflow.com/questions/7569335/reverse-a-string-in-java), and convert it back to an `int`(if it's necessary). – Christian Tapia Jan 26 '15 at 15:00
  • 1
    but it's probably same complexity :) – Mysterion Jan 26 '15 at 15:01
  • 6
    This is probably better suited to [codereview.stackexchange.com](http://codereview.stackexchange.com) – Holloway Jan 26 '15 at 15:04
  • 3
    @Mysterion, not really. It can look nicer, but just to show how while compressing it all into a single line to fit into the comments: Integer.parseInt(new StringBuilder(Integer.toString(new Integer(123456789))).reverse().toString()); – Ofer Lando Jan 26 '15 at 15:06
  • 2
    @OferLando this fails if the result of the reversed `int` doesn't fit in `int` values e.g. try reversing `2147483647` – Luiggi Mendoza Jan 26 '15 at 15:08
  • @LuiggiMendoza PSB: the question didn't ask about bigger values, but regardless, this can be resolved using BigInteger just as well. – Ofer Lando Jan 26 '15 at 15:44
  • @OferLando I suggest you to check the most voted answer from the duplicate Q/A. – Luiggi Mendoza Jan 26 '15 at 15:44
  • @LuiggiMendoza it's nice, but it's actually more complicated. – Ofer Lando Jan 26 '15 at 15:48
  • @OferLando it's the same algorithm OP's following but use a `long` to store the result, then check if the resultant `long` is between a valid `int` value and handle this case. I don't see how this is *more complicated*. – Luiggi Mendoza Jan 26 '15 at 15:51
  • @LuiggiMendoza, I didn't say it is more complicated than what Sanjay documented - it is more complicated that what Christian and I suggested. But anyway, OP's question was answered, so this discussion is pretty much redundant. – Ofer Lando Jan 26 '15 at 15:54

3 Answers3

1

As @Christian mentioned above in the comments, you can do something like this:

int originalInt = 123456789;
String str = new StringBuilder(Integer.toString(originalInt)).reverse().toString();
int reversedInt = Integer.parseInt(str);

Or as a one-liner:

Integer.parseInt(new StringBuilder(Integer.toString(new Integer(123456789))).reverse().toString());

EDIT:

To answer @Luiggi Mendoza's concern - supporting numbers that are too big to fit an Integer can be done using BigInteger as follows:

BigInteger originalValue = BigInteger.valueOf(563463346233535772l);
String reversedString = new StringBuilder(originalValue.toString()).reverse().toString();
Ofer Lando
  • 814
  • 7
  • 12
0
String myNum=num.toString();
String reversed = new StringBuffer(myNum).reverse().toString();
num=Integer.parseInt(reversed);
Harun ERGUL
  • 5,770
  • 5
  • 53
  • 62
0

Example without StringBuilder.

private static long revert(long number){
    String numberAsString = String.valueOf(number);

    String[] splitted = numberAsString.split("");

    String returnString = "";

    for(int i = splitted.length-1; i >=0; i--){
        returnString += splitted[i];
    }

    return Integer.valueOf(returnString);
}
Christian
  • 22,585
  • 9
  • 80
  • 106