Reverse Number Program in Java
Last Updated :
14 Jul, 2023
In Java, reversing a number means that the digit at the first position should be swapped with the last digit, the second digit will be swapped with the second last digit, and so on till the middle element.
Note: Leading zeros will not be considered after reversing (i.e, After reversing 100 will become 001 ≈ 1).
Algorithm for Reversing a Number in Java
To reverse a number following steps should be performed:
- Take the number’s modulo by 10.
- Multiply the reverse number by 10 and add modulo value into the reverse number.
- Divide the number by 10.
- Repeat above steps until number becomes zero.
Methods to Reverse a Number in Java
We can reverse a number in Java using three main methods as mentioned below:
- Using While Loop
- Using Recursion
- Using Stringbuilder class
1. Reverse a Number Using the While Loop
Simply apply the steps/algorithm discussed and terminate the loop when the number becomes zero.
Below is the implementation of the above approach:
Java
import java.io.*;
class GFG {
static int reverse( int n)
{
int rev = 0 ;
int rem;
while (n > 0 ) {
rem = n % 10 ;
rev = (rev * 10 ) + rem;
n = n / 10 ;
}
return rev;
}
public static void main(String[] args)
{
int n = 4526 ;
System.out.print( "Reversed Number is "
+ reverse(n));
}
}
|
Output
Reversed Number is 6254
The complexity of the above method
Time complexity: O(log10n) for given number n
Auxiliary space: O(1)
2. Using Recursion for Reversing a Number in Java
In recursion, the final reverse value will be stored in the global ‘rev’ variable. Follow the below instructions.
- If the number becomes zero then terminate the recursion, this will be the base condition.
- Take the modulo and add it with the ‘rev*10’ multiplying.
- Divide the number by 10 and call the reverse function on this number after updating it to number/10.
Below is the Java implementation of the recursion method:
Java
import java.io.*;
class GFG {
static int rev = 0 ;
static void reverse( int n)
{
if (n <= 0 )
return ;
int rem = n % 10 ;
rev = (rev * 10 ) + rem;
reverse(n / 10 );
}
public static void main(String[] args)
{
int n = 4526 ;
reverse(n);
System.out.print( "Reversed Number is " + rev);
}
}
|
Output
Reversed Number is 6254
The complexity of the above method
Time Complexity : O(logn) ,where n is number
Auxiliary Space: O(logn)
3. Reverse a Number in Java Using the StringBuilder class
In this approach, we are going to StringBuilder class to reverse the number. We will use to StringBuilder Reverse method.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int n = 123456 ;
String temp = "" + n;
StringBuilder sb = new StringBuilder(temp);
StringBuilder str = sb.reverse();
System.out.println(str.toString());
}
}
|
The complexity of the above method
Time Complexity : O(ln)
Auxiliary Space: O(n)
Please refer to the complete article for more information – Write a program to reverse digits of a number
Share your thoughts in the comments
Please Login to comment...