Open In App

Reverse Number Program in Java

Improve
Improve
Like Article
Like
Save
Share
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.

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




// Java program to reverse a number
import java.io.*;
  
// Driver Class
class GFG {
    // 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 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




// Java program to reverse
// a number recursively
import java.io.*;
  
class GFG {
    // 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. 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




// Java Program to reverse a Number
// Using a StringBuilder Class
import java.util.*;
  
// Driver Class
public class GFG {
    // 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



Last Updated : 14 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads