Way 1 : reverse an integer in Java:
To reverse an integer in Java, you can use the following approach:
public class IntegerReverseExample {
public static int reverse(int number) {
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
return reversed;
}
public static void main(String[] args) {
int number = 12345;
int reversedNumber = reverse(number);
System.out.println("Original number: " + number);
System.out.println("Reversed number: " + reversedNumber);
}
}
Output:
Original number: 12345
Reversed number: 54321
In this code, the reverse()
method takes an integer number
as input and reverses it. It initializes a variable reversed
to 0 to store the reversed number.
The method enters a loop while the number
is not equal to 0. In each iteration, it retrieves the rightmost digit of the number
using the modulo operator %
and assigns it to the digit
variable. It then updates the reversed
number by multiplying it by 10 and adding the digit
. Finally, it divides the number
by 10 to remove the rightmost digit.
This process continues until the number
becomes 0, and the loop terminates. The method then returns the reversed
number.
By reversing the digits of the original number, you can obtain the reversed integer.
Way 2 : reverse an integer in Java: by converting it to a string.
Another way to reverse an integer in Java is by converting it to a string, reversing the string, and then converting it back to an integer. Here’s an example:
public class IntegerReverseExample {
public static int reverse(int number) {
String numberString = String.valueOf(number);
StringBuilder reversedString = new StringBuilder(numberString).reverse();
String reversedNumberString = reversedString.toString();
int reversedNumber = Integer.parseInt(reversedNumberString);
return reversedNumber;
}
public static void main(String[] args) {
int number = 12345;
int reversedNumber = reverse(number);
System.out.println("Original number: " + number);
System.out.println("Reversed number: " + reversedNumber);
}
}
Output:
Original number: 12345
Reversed number: 54321
In this approach, the reverse()
method takes an integer number
as input and reverses it. It first converts the number
to a string using String.valueOf(number)
. Then, it creates a StringBuilder
object and initializes it with the reversed version of the string using the reverse()
method of StringBuilder
. After obtaining the reversed string, it converts it back to an integer using Integer.parseInt(reversedNumberString)
.
By converting the integer to a string, reversing the string, and then converting it back to an integer, you can achieve the reversed integer.
Way 3 : reverse an integer in Java: using arithmetic operations.
another way to reverse an integer in Java is by using arithmetic operations. Here’s an example:
public class IntegerReverseExample {
public static int reverse(int number) {
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
return reversed;
}
public static void main(String[] args) {
int number = 12345;
int reversedNumber = reverse(number);
System.out.println("Original number: " + number);
System.out.println("Reversed number: " + reversedNumber);
}
}
Output:
Original number: 12345
Reversed number: 54321
This approach is the same as the one mentioned earlier. It uses arithmetic operations to reverse the digits of the number. The reverse()
method takes an integer number
as input and initializes a variable reversed
to 0 to store the reversed number.
The method enters a loop while the number
is not equal to 0. In each iteration, it retrieves the rightmost digit of the number
using the modulo operator %
and assigns it to the digit
variable. It then updates the reversed
number by multiplying it by 10 and adding the digit
. Finally, it divides the number
by 10 to remove the rightmost digit.
This process continues until the number
becomes 0, and the loop terminates. The method then returns the reversed
number.
By reversing the digits of the original number, you can obtain the reversed integer.
Way 4 : reverse an integer in Java: by converting it to a character array, swapping the elements of the array.
Another way to reverse an integer in Java is by converting it to a character array, swapping the elements of the array, and then converting it back to an integer. Here’s an example:
public class IntegerReverseExample {
public static int reverse(int number) {
char[] digits = String.valueOf(number).toCharArray();
int start = 0;
int end = digits.length - 1;
while (start < end) {
char temp = digits[start];
digits[start] = digits[end];
digits[end] = temp;
start++;
end--;
}
String reversedString = new String(digits);
int reversedNumber = Integer.parseInt(reversedString);
return reversedNumber;
}
public static void main(String[] args) {
int number = 12345;
int reversedNumber = reverse(number);
System.out.println("Original number: " + number);
System.out.println("Reversed number: " + reversedNumber);
}
}
Output:
Original number: 12345
Reversed number: 54321
In this approach, the reverse()
method takes an integer number
as input and reverses it. It first converts the number
to a string using String.valueOf(number)
. Then, it converts the string to a character array using toCharArray()
.
The method uses two pointers, start
and end
, initialized to the start and end indices of the character array, respectively. It swaps the characters at the start
and end
indices iteratively while incrementing start
and decrementing end
. This process continues until the start
pointer surpasses the end
pointer.
After reversing the characters in the character array, it converts the array back to a string using new String(digits)
. Finally, it parses the reversed string as an integer using Integer.parseInt(reversedString)
and returns the reversed number.
By converting the integer to a character array, swapping the elements, and converting it back to an integer, you can achieve the reversed integer.
You can run above all code here just copy and paste.