Java Program to find out the reverse of a number using recursion

Here we are explaining how to write a java program to find out the reverse of a number. So first we have to read the number from the user. Then we will find out the length of the number and call the recursive function reverse() to find out the reverse of the number.

What is the reverse of a number?

Suppose the number is 563 then the reverse of the number is 365. The reverse of a number is, just reverse the digits of that number.

How to implement the java program to find out the reverse of a number using recursion?

First, we have to declare the class RevOfNum.Then declare the integer variables n, count, a. The variable n is used to hold the number, the count is used to hold the length of the number. Create an object of the scanner class sc and read the integer number from the user into the variable n.By using a while loop find out the number of digits in the number into the variable count. Create an object of the class RevOfNum as obj and call the recursive function reverse(n, count) and display the reverse of the number as b.

In the function reverse(int x,int y),check if y=1,if true then return x.Else calculate z=x,x=x/10 and return (int) ((z * pow(10, y - 1)) + reverse(x, --y))

ALGORITHM

STEP 1: Declare the class RevOfNum with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variable n,count,a and set count=0.

STEP 4: Read the number into the variable n.

STEP 5: Assign a to n.

STEP 6: By using a while loop with the condition a>0 do step 7.

STEP 7: Increment count by 1.

STEP 8: Divide a by 10 and repeat step 6.

STEP 9: Create an object of the class RevOfNum as obj.

STEP 10:Call the recursive function obj.reverse(n,count)

STEP 11: Display the Reverse of the number as b.

FUNCTION reverse(int x,int y)

STEP 1:Check if y=1 then return x.Else do step 2.

STEP 2: Calculate z=x ,x=x/10.

STEP 3:Return (int) ((z * pow(10, y - 1)) + reverse(x, --y))

 

Java Source Code

                                          import static java.lang.StrictMath.pow;
import java.util.Scanner;
public class RevOfNum 
{
    public static void main(String[] args) 
    {
        int n, count = 0, a;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number:");
        n = sc.nextInt();
        a = n;
        while(a > 0)
        {
            count++;
            a = a / 10;
        }
        RevOfNum obj = new RevOfNum();
        int b = obj.reverse(n, count);
        System.out.println("Reverse of the number is :"+b);
    }
    int reverse(int x, int y)
    {
        if(y== 1)
        {
            return x;
        }
        else
        {
            int z = x % 10;
            x = x / 10;
            return (int) ((z * pow(10, y - 1)) + reverse(x, --y));
        }
    }
}
                                      

OUTPUT

Enter the number:
12345
Reverse of the number is :54321