Java Program to Reverse a Number by Using Recursion

In the previous article, we have discussed about Java Program to Find Nth Power of a Number by Using Recursion

In this program we are going to see how to reverse a number using recursion by Java programming language.

Java Program to Reverse a Number by Using Recursion

Let’s see an example to understand it more clearly.

Assume a number n = 94371
Then the reverse of the number = 17349

Now let’s see different ways to reverse a number by using Recursion.

Method-1: Java Program to Reverse a Number By Using Static Input and Recursion

Approach:

  • Declare an integer variable ‘n’ and initialize it.
  • Call a user defined method reverseNumber() method and pass the Integer ‘n’ as parameter.
  • Inside the user defined method we will check if the the number is 1 digit number then print the same number ‘n’.
  • Else if the number is greater than 1 digit then recursively call reverseNumber() method to extract the digits and print it.

Program:

public class Main 
{
    public static void main(String[] args)
    {
        //declare an integer variable ‘n’ and initialize it
        int n = 94371;
        System.out.println("The reverse of the number "+n+" is:");
        //calling reverseNumber() method to reverse the number
        reverseNumber(n);
    }

    //recursive function to reverse a number        
    static void reverseNumber(int n)
    {
        // if the number is 1 digit number then print the same number
        if(n<10)
        System.out.print(n);
        // if the number is more than 1 digit number then recursively call reverseNumber() method and print the remainder
        else
        {
    	    System.out.print(n%10);
            reverseNumber(n/10);
        } 
    }
}
Output:

The reverse of the number 94371 is:
17349

Method-2: Java Program to Reverse a Number By Using User Input and Recursion

Approach:

  • Create a scanner class.
  • Declare an integer variables say ‘n
  • Prompt the user to enter the values for the integer.
  • Declare an integer variable ‘n’ and initialize it.
  • Call a user defined method reverseNumber() method and pass the integer ‘n’ as parameter.
  • Inside the user defined method we will check if the the number is 1 digit number then print the same number ‘n’.
  • Else if the number is greater than 1 digit then recursively call reverseNumber() method to extract the digits and print it.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        //declare an integer variable ‘n’and initialize it by user input using scanner class.
        int n = sc.nextInt();
        System.out.println("The reverse of the number "+n+" is:");
        //calling reverseNumber() method to reverse the number
    	reverseNumber(n);
    }

    //recursive function to reverse a number        
    static void reverseNumber(int n)
    {
        // if the number is 1 digit number then print the same number
        if(n<10)
        System.out.print(n);
        // if the number is more than 1digit number then recursively call reverseNumber() method and print the remainder
        else
        {
            System.out.print(n%10);
            reverseNumber(n/10);
        } 
    }
}
Output:

Enter a number: 
73810
The reverse of the number 73810 is:
01837

If you are new to Java and want to learn the Java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: