Open In App

Reverse Number Program in Java

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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));
    }
}

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

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);
    }
}

Output
Reversed 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());
    }
}

Output
654321

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



Next Article
Article Tags :
Practice Tags :

Similar Reads