Reverse and Add Function in Java


We are given with an integer and the agenda here is to reverse the digits of the number and add the reversed number to the original number and check if the resultant number is a palindrome or not and the process is repeated until it does. The breaking point of the process is 1000 iterations and a value greater than the maximum long value( Long.MAX_VALUE).

For Examples

Input − 1678

Output − Palindrome of the given input 1678 293392

Explanation − The input number is first reversed and then added to the original number, it is then checked for palindrome if it is not a palindrome then the same process is repeated on the updated number. The breaking point of the process is 1000 iterations and a value greater than the maximum long value( Long.MAX_VALUE).

Input − 202021038

Output − Palindrome of the given input 202021038 1453553541

Explanation − The input number is first reversed and then added to the original number, it is then checked for palindrome if it is not a palindrome then the same process is repeated on the updated number. The breaking point of the process is 1000 iterations and a value greater than the maximum long value( Long.MAX_VALUE).

Approach used in the below program is as follows

  • Inside the main function

    • The input number is passed in the method calculateReverseandAdd(input)

  • Inside the method calculateReverseandAdd

    • A new variable rev_number is introduced

    • A loop is iterated with the condition number <= max

    • Inside the loop, the reverse of the number is obtained by passing the number through the method reverseNumber()

    • The reversed number is then added to the input number

    • The obtained number is then checked for palindrome by passing the number in the method checkPalindrome(), If yes the number is printed as output to the user.

    • If the number is not palindrome then the same process is repeated with the obtained number till a palindrome is obtained, if the resultant number is greater than the maximum long value then no possible palindrome is in the range for the given number exist.

Example

import java.util.*;
public class ReverseAdd{
   static final long max = Long.MAX_VALUE;
   static long reverseNumber(long number){
      long rev_number = 0;
      while (number > 0){
         rev_number = rev_number * 10 + number % 10;
         number = number / 10;
      }
      return rev_number;
   }
   static boolean checkPalindrome(long number){
      return (reverseNumber(number) == number);
   }
   static void calculateReverseandAdd(long number){
      long rev_number = 0;
      System.out.println("Palindrome of the given input " + number);
      while (number <= max){
         rev_number = reverseNumber(number);
         number = number + rev_number;
         if (checkPalindrome(number)){
            System.out.println(number);
            break;
         }
         else if (number > max){
            System.out.println("No possible palindromes for the input");
         }
      }
   }
   public static void main(String[] args){
      calculateReverseandAdd(1678);
      calculateReverseandAdd(2961);
      calculateReverseandAdd(202021038);
   }
}

Output

If we run the above code it will generate the following Output

Palindrome of the given input 1678
293392
Palindrome of the given input 2961
69696
Palindrome of the given input 202021038
1453553541

Updated on: 03-Nov-2021

521 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements