Reverse Number Program in Java
Last Updated :
08 Apr, 2025
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.
Example of reversing a number:
Input: n = 357
Output: 753
Input n = 100
Output: 1 ( leading zeros are not considered )
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, the 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 the above steps until the 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. Using While Loop
Simply apply the steps/algorithm discussed and terminate the loop when the number becomes zero.
Example:
Java
// Java program to reverse a number
import java.io.*;
// Driver Class
class Geeks
{
// Function to reverse the number
static int reverse(int n)
{
// reversed number
int rev = 0;
// remainder
int rem;
while (n > 0) {
rem = n % 10;
rev = (rev * 10) + rem;
n = n / 10;
}
return rev;
}
// Driver Function
public static void main(String[] args)
{
int n = 4526;
System.out.print("Reversed Number is: "
+ reverse(n));
}
}
OutputReversed Number is: 6254
The complexity of the above method:
Time complexity: O(log10n) for given number n
Auxiliary space: O(1)
2. Using Recursion
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.
Example:
Java
// Java program to reverse
// a number recursively
import java.io.*;
class Geeks
{
// stores reversed number
static int rev = 0;
// Function to reverse the number
static void reverse(int n)
{
if (n <= 0)
return;
// remainder
int rem = n % 10;
rev = (rev * 10) + rem;
reverse(n / 10);
}
// Driver Function
public static void main(String[] args)
{
int n = 4526;
reverse(n);
System.out.print("Reversed Number is: " + rev);
}
}
OutputReversed Number is: 6254
The complexity of the above method:
Time Complexity : O(logn) ,where n is number
Auxiliary Space: O(logn)
3. Using StringBuilder Class
In this approach, we are going to StringBuilder class to reverse the number. We will use to StringBuilder reverse() method.
Example:
Java
// Java Program to reverse a Number
// using a StringBuilder Class
import java.util.*;
// Driver Class
public class Geeks
{
// main function
public static void main(String[] args)
{
int n = 123456;
// conversion of int to string
String temp = "" + n;
// creating stringbuilder obj
StringBuilder sb = new StringBuilder(temp);
// using reverse method to
// reverse the obj
StringBuilder str = sb.reverse();
// printing reverse number
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
How to Reverse a Number in Java
Similar Reads
Java Program to Reverse a List Reversing a list means after reversing the list, the first element should be swapped with the last element, the second element should be swapped with the second last element, and so on till the middle element and the resultant list is the reversed list. Example: Input: "PLATFORM", "LEARNING", "BEST"
3 min read
Java Program for Reverse a linked list Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In
3 min read
Palindrome Number Program in Java A given number can be said to be palindromic if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.Example of Palindrome Number:Input : n = 121Output: Reverse of n = 121Palindrome : YesIn
7 min read
Java Program to Find Reverse of a Number Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Step 1: Assume the smaller problem from the problem that is similar to the bigger/origi
5 min read
Java Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p
4 min read
Java Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes,
5 min read