Java Program to Reverse a Number using Recursion

Here is a Java program to reverse a number using recursion. To reverse a number we have to reverse the the sequence of digits in a number. After reversing, least significant digit(right most digit) will become most significant digit() of number and most significant digit becomes least significant digit and so on.

For Example:
Befor reverse : 745362
After reverse : 263547
Here, we will reverse the digits of of a number using reversion using below mentioned recursive equation.

getReversedNumber(N) = lastDigitOf(N) x power(10, numberOfDigits(N/10))
                         + getReversedNumber(N/10)
Gere is the detailed explanation of above algorithm.
Algorithm to reverse a number using recursion
  • lastDigitOf(N) : This function returns the least significant digit of N. For Example :lastDigitOf(1234) = 4.

  • power(A, B) : This function returns the value of AB. For Example: power(2,4) = 16.

  • numberOfDigits(N) : This function returns the number of digits in N. log10(N) + 1, gives us the number of digits in N. For Example: numberOfDigits(1234) = log10(1234) + 1 = 4

  • getReversedNumber(N) = This is a recursive function, which reverses the sequence of digits of N and returns it to the caller function.
For Example
  • getReversedNumber(1234) = lastDigitOf(1234) x power(10, numberOfDigits(1234/10)) + getReversedNumber(1234/10)
  • getReversedNumber(1234) = 4 x power(10, numberOfDigits(123)) + getReversedNumber(123)
  • getReversedNumber(1234) = 4 x power(10, 3) + 321
  • getReversedNumber(1234) = 4 x 1000 + 321
  • getReversedNumber(1234) = 4321

  • Java program Reverse digits of a number using recursion

    package com.tcc.java.programs;
    
    import java.io.*;
    import java.util.*;
    
    public class ReverseNumberRecursion {
        public static void main(String[] args) {
            int number=0;
     System.out.println("Input an Integer");
     Scanner in = new Scanner(System.in);
     number = in.nextInt();
     System.out.print("Reversed Number : " + reverse(number));
        }
    
        public static int reverse(int number) {
            int lastDigit, numberOfDigits, sign = 1;
         
            if(number < 0){
                number = number * -1;
                sign = -1;
            }
            if(number < 10)
                return number*sign;
             
            lastDigit = number % 10;
            number = number / 10;
            /* log10(number) + 1 gives number of digits in number */
            numberOfDigits = (int) (Math.log10((double)number) + 1);
         
            return (int)(lastDigit*Math.pow((double)10,(doublumberOfDigits) 
             + reverse(number)) * sign;
        }
    }
    
    Output
    Input an Integer
    435203
    Reversed Number : 302534
    
    Input an Integer
    4
    Reversed Number : 4
    

    Recommended Posts
    Java Program To Reverse Digits of a Number using While Loop
    Java Program to Find All Factors of a Number
    Java Program to Check a Number is Prime Number or Not
    Java Program to Check Whether a Number is Palindrome or Not
    Java Program to Find Sum of Digits of a Number
    Java Program to Count Number of Digits in a Number
    Java Program to Find Factorial of a Number Using Recursion
    Java Program to Print Multiplication Table of Number
    Java Program to Check If a Year is Leap Year or Not
    Java Program to Calculate Simple Interest
    Java Program to Find LCM and GCD of Two Numbers
    All Java Programs