Java reverse an int value without using array

2019-01-02 18:03发布

Can anyone explain to me how to reverse an integer without using array or String. I got this code from online, but not really understand why + input % 10 and divide again.

while (input != 0) {
    reversedNum = reversedNum * 10 + input % 10;
    input = input / 10;   
}

And how to do use this sample code to reverse only odd number. Example I got this input 12345, then it will reverse the odd number to output 531.

29条回答
弹指情弦暗扣
2楼-- · 2019-01-02 18:28

Even if negative integer is passed then it will give the negative integer Try This...

public int reverse(int result) {

    long newNum=0,old=result;
    result=(result>0) ? result:(0-result);

    while(result!=0){
        newNum*=10;
        newNum+=result%10;
        result/=10;
        if(newNum>Integer.MAX_VALUE||newNum<Integer.MIN_VALUE)
            return 0;
    }
    if(old > 0)
        return (int)newNum;
    else if(old < 0)
        return (int)(newNum*-1);
    else 
        return 0;
}
查看更多
闭嘴吧你
3楼-- · 2019-01-02 18:28

This is the shortest code to reverse an integer

int i=5263; 
System.out.println(Integer.parseInt(new StringBuffer(String.valueOf(i) ).reverse().toString()));
查看更多
妖精总统
4楼-- · 2019-01-02 18:29

If you wanna reverse any number like 1234 and you want to revers this number to let it looks like 4321. First of all, initialize 3 variables int org ; int reverse = 0; and int reminder ; then put your logic like

    Scanner input = new Scanner (System.in);
    System.out.println("Enter number to reverse ");
    int org = input.nextInt();
    int getReminder;
    int r = 0;
    int count = 0;

    while (org !=0){
        getReminder = org%10;
         r = 10 * r + getReminder;
         org = org/10;



    }
        System.out.println(r);

    }
查看更多
素衣白纱
5楼-- · 2019-01-02 18:30
while (input != 0) {
  reversedNum = reversedNum * 10 + input % 10;
  input = input / 10;
}

let a number be 168,
+ input % 10 returns last digit as reminder i.e. 8 but next time it should return 6,hence number must be reduced to 16 from 168, as divide 168 by 10 that results to 16 instead of 16.8 as variable input is supposed to be integer type in the above program.

查看更多
余生无你
6楼-- · 2019-01-02 18:30

A method to get the greatest power of ten smaller or equal to an integer: (in recursion)

public static int powerOfTen(int n) {
    if ( n < 10)
        return 1;
    else
        return 10 * powerOfTen(n/10); 
}

The method to reverse the actual integer:(in recursion)

public static int reverseInteger(int i) {
    if (i / 10 < 1)
        return i ;
    else
        return i%10*powerOfTen(i) + reverseInteger(i/10);
}
查看更多
初与友歌
7楼-- · 2019-01-02 18:32

If the idea is not to use arrays or string, reversing an integer has to be done by reading the digits of a number from the end one at a time. Below explanation is provided in detail to help the novice.

pseudocode :

  1. lets start with reversed_number = 0 and some value for original_number which needs to be reversed.
  2. the_last_digit = original_number % 10 (i.e, the reminder after dividing by 10)
  3. original_number = original_number/10 (since we already have the last digit, remove the last digit from the original_number)
  4. reversed_number = reversed_number * 10 + last_digit (multiply the reversed_number with 10, so as to add the last_digit to it)
  5. repeat steps 2 to 4, till the original_number becomes 0. When original_number = 0, reversed_number would have the reverse of the original_number.

More info on step 4: If you are provided with a digit at a time, and asked to append it at the end of a number, how would you do it - by moving the original number one place to the left so as to accommodate the new digit. If number 23 has to become 234, you multiply 23 with 10 and then add 4.

234 = 23x10 + 4;

Code:

public static int reverseInt(int original_number) {
        int reversed_number = 0;
        while (original_number > 0) {
            int last_digit = original_number % 10;
            original_number = original_number / 10;
            reversed_number = reversed_number * 10 + last_digit;    
        }
        return reversed_number;
    }
查看更多
登录 后发表回答