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:47
int convert (int n)
{
        long val = 0;

        if(n==0)
            return 0;

        for(int i = 1; n > exponent(10,  (i-1)); i++)
        {
            int mod = n%( (exponent(10, i))) ;
            int index = mod / (exponent(10, i-1));

            val *= 10;
            val += index;
        }

        if (val < Integer.MIN_VALUE || val > Integer.MAX_VALUE) 
        {
            throw new IllegalArgumentException
                (val + " cannot be cast to int without changing its value.");
        }
        return (int) val;

    }


static int exponent(int m, int n)
    {
        if(n < 0) 
            return 0;
        if(0 == n) 
            return 1;

        return (m * exponent(m, n-1));

    }
查看更多
其实,你不懂
3楼-- · 2019-01-02 18:49

Simply you can use this

public int getReverseInt(int value) {
    int resultNumber = 0;
    for (int i = value; i !=0; i /= 10) {
        resultNumber = resultNumber * 10 + i % 10;
    }
    return resultNumber;        
}

You can use this method with given value which you want revers.

查看更多
梦寄多情
4楼-- · 2019-01-02 18:49
public static double reverse(int num)
{
    double num1 = num;
    double ret = 0;
    double counter = 0;

    while (num1 > 1)
    {   
        counter++;
        num1 = num1/10;
    }
    while(counter >= 0)
    {
        int lastdigit = num%10;
        ret += Math.pow(10, counter-1) * lastdigit;
        num = num/10;
        counter--;  
    }
    return ret;
}
查看更多
浮光初槿花落
5楼-- · 2019-01-02 18:50

I used String and I converted initially the int to String.Then I used the reverse method. I found the reverse of the number in String and then I converted the string back to int. Here is the program.

import java.util.*;

public class Panathinaikos {
    public static void my_try()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the number you want to be reversed");
        int number = input.nextInt();
        String sReverse = Integer.toString(number);
        String reverse = new StringBuffer(sReverse).reverse().toString();
        int Reversed = Integer.parseInt(reverse);
        System.out.print("The number " + number+ " reversed is " + Reversed);
    }
}
查看更多
浅入江南
6楼-- · 2019-01-02 18:51
while (num != 0) {
    rev = rev * 10 + num % 10;
    num /= 10;
}

That is the solution I used for this problem, and it works fine. More details:

num % 10

This statement will get you the last digit from the original number.

num /= 10

This statement will eliminate the last digit from the original number, and hence we are sure that while loop will terminate.

rev = rev * 10 + num % 10

Here rev*10 will shift the value by left and then add the last digit from the original.
If the original number was 1258, and in the middle of the run time we have rev = 85, num = 12 so:
num%10 = 2
rev*10 = 850
rev*10 + num%10 = 852

查看更多
无色无味的生活
7楼-- · 2019-01-02 18:51
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class intreverse
{
public static void main(String...a)throws Exception
{
    int no;
    int rev = 0;
    System.out.println("Enter The no to be reversed");
    InputStreamReader str=new InputStreamReader(System.in);
    BufferedReader br =new BufferedReader(str);
    no=Integer.parseInt(br.readLine().toString());
    while(no!=0)
    {
        rev=rev*10+no%10;
        no=no/10;

    }
    System.out.println(rev);
}
}
查看更多
登录 后发表回答