Java Reverse an Int Value Without Using Array

Java reverse an int value without using array

I am not clear about your Odd number.
The way this code works is (it is not a Java specific algorithm)
Eg.
input =2345
first time in the while loop
rev=5 input=234
second time
rev=5*10+4=54 input=23
third time
rev=54*10+3 input=2
fourth time
rev=543*10+2 input=0

So the reversed number is 5432.
If you just want only the odd numbers in the reversed number then.
The code is:

while (input != 0) {    
last_digit = input % 10;
if (last_digit % 2 != 0) {
reversedNum = reversedNum * 10 + last_digit;

}
input = input / 10;
}

Reverse the digits of an integer

Your code assumes that the number can be divided by 1000, which is clearly not the case for numbers below 1000. So add some if statements:

public int reverseNumber(int n) {
// step one: we find the factors using integer maths
int s = n;
int thousands = s / 1000; // this will be 0 if the number is <1000
s = s - thousands*1000;
int hundreds = s / 100; // this will be 0 if the number is <100
s = s - hundreds*100;
int tens = s / 10; // etc.
s = s - tens*10;
int ones = s;

// then: let's start reversing. single digit?
if (n<10) return n;

// two digits?
if (n<100) {
return ones*10 + tens;
}

// etc.
if (n<1000) {
return ones*100 + tens*10 + hundreds;
}

if (n<10000) {
return ones*1000 + tens*100 + hundreds*10 + thousands;
}

// if we get here, we have no idea what to do with this number.
return n;
}

Java reverse an int value without using array

I am not clear about your Odd number.
The way this code works is (it is not a Java specific algorithm)
Eg.
input =2345
first time in the while loop
rev=5 input=234
second time
rev=5*10+4=54 input=23
third time
rev=54*10+3 input=2
fourth time
rev=543*10+2 input=0

So the reversed number is 5432.
If you just want only the odd numbers in the reversed number then.
The code is:

while (input != 0) {    
last_digit = input % 10;
if (last_digit % 2 != 0) {
reversedNum = reversedNum * 10 + last_digit;

}
input = input / 10;
}

How do I reverse an int array in Java?

To reverse an int array, you swap items up until you reach the midpoint, like this:

for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}

The way you are doing it, you swap each element twice, so the result is the same as the initial list.

Reverse an integer without losing 0's in input

Integers store an integer value, not an encoding of a particular representation of an integral value. So in order to really get what you want you're talking about reversing the representation of an integer.

EDIT: I thought I'd add some code to solve the solution using Strings.

public static int reverseInputNumber(int num)
{
String reverse = StringBuilder(num + "").reverse().toString();
return reverse;
}

Second Edit: Actually even this code won't do what you want it to do. By taking an int as a parameter you're already losing the original representation of the integer. It's accurate to say that there is no function that can take just an integer parameter and return something with the reverse of it's original representation. You would need to deal solely in strings or some other more appropriate data structure.

How to reverse int array Java

In the for-loop you are flipping the first number and the last number, the second number and the second-to-last number, the third number and the third-to-last number, and so on. If you go from start to the end you will flip each number twice, because you move fully though the array, and flip two numbers, therefore flipping twice the numbers in the array. You must only traverse half the array, ex:

int counter = 0;
for(int i = numArray.length-1; i >= counter; i--){
int temp = numArray[i];
numArray[i] = numArray[numArray.length - i - 1];
numArray[numArray.length - i - 1] = temp;
//print the reversed array
//System.out.print("index " + (i) + ": "+ numArray[i] + ", ");
counter++;
}

//print arr
// must be separate because must iterate the whole array
for(int i = 0; i < numArray.length; ++i){
System.out.print("Index: "+i+": "+numArray[i]+", ");
}
System.out.println(""); // end code with newline


How to reverse a long value and handle overflow cases in java?

As mentioned by @qwertyman in comments. This solution works fine.

public long reverseLong(long value){

int sign = 1;
long result=0; //to hold result

if(value < 0){
value =-value;
sign = -1;
}

while(value !=0){
/*
* To give a little perspective about how one can arrive at writing this
* condition:- Think of long as some type which holds only 2 or 3 bits of
* data. If it holds only 2 bits, then range of values it can hold will be
* 0 to 3. Now it's easy to validate or arrive at conditions like the one
* in below.
*/
if(result > (Long.MAX_VALUE - value%10)/10){
throw new NumberFormatException();
}
result = result*10+value%10;
value = value/10;
}

return sign*result;
}


Related Topics



Leave a reply



Submit