Skip to content
Related Articles

Related Articles

Improve Article
Java Program to Reverse a Number & Check if it is a Palindrome
  • Difficulty Level : Easy
  • Last Updated : 04 Nov, 2020

Given number can be said palindromic in nature if the reverse of the given number is the same as that of a given number. Length of number is log10(n), i.e. For BigIntegers using string operation like creation reverse and check palindrome will take log10(n) time. 

A. Number under Java int limit 

Example : 

Input : n = 46355364
Output: Reverse of n = 46355364
    Palindrome = Yes

Input : n = 87476572465
Output: Reverse of n = 56427567478
    Palindrome = No

We can reverse a number in multiple ways, below is the iterative implementation for the same.

Iterative Algorithm :



Input:  number
1. Initialize reversed_number = 0
2. Loop while num
ber > 0
     a. Multiply reversed_number by 10 and add number  % 10 to reversed_number
               reversed_number  = reversed_number*10 + number %10;
     b. Divide number by 10
3. Return reversed_number

Example:

number = 1234
reversed_number= 0

reversed_number = reversed_number *10 + number%10 = 4
number = number/10 = 123

reversed_number = reversed_number*10 + number%10 = 20 + 6 = 43
number = number/10 = 12

reversed_number = reversed_number*10 + number%10 = 260 + 5 = 432
number = number/10 = 1

reversed_number = reversed_number*10 + number%10 = 265 + 4 = 4321
number = number/10 = 0

Below is the implementation of the above approach.
 

Java




// Java program to reverse a number
// and find if it is a palindrome or not
class GFG {
  
    // Iterative function to
    // reverse the digits of number
    static int reversNumber(int n)
    {
        int reversed_n = 0;
        while (n > 0) {
            reversed_n = reversed_n * 10 + n % 10;
            n = n / 10;
        }
        return reversed_n;
    }
  
    // Main function
    public static void main(String[] args)
    {
        int n = 123464321;
        int reverseN = reversNumber(n);
        System.out.println("Reverse of n = " + reverseN);
        
        // Checking if n is same
        // as reverse of n
        if (n == reverseN)
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}
Output
Reverse of n = 123464321
Palindrome = Yes

Time Complexity: O(log10(n)) where n is the input number.

B. Large integer: Use BigInteger Class

Example

Input : n = 12345678999999999987654321
Output: Reverse of n = 12345678999999999987654321
    Palindrome = Yes

Approach

  1. Take input in BigInteger variable.
  2. Reverse the given BigInteger using the reverse method.
  3. Compare both BigIntegers using compareTo() method.

Below is the implementation of the above approach.

Java




// Java program to reverse a number
// and find if it is a palindrome or not
import java.io.*;
import java.math.BigInteger;
class GFG {
    // Reverse Big Integer
    public static BigInteger reverse(BigInteger n)
    {
        String s = n.toString();
        StringBuilder sb = new StringBuilder(s);
        return new BigInteger(sb.reverse().toString());
    }
    // Main Function
    public static void main(String[] args)
    {
        BigInteger n
            = new BigInteger("12345678999999999987654321");
        BigInteger reverseN = reverse(n);
        System.out.println("Reverse of n = " + reverseN);
  
        // Checking if n is same
        // as reverse of n
        if (n.compareTo(reverseN) == 0)
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}
Output
Reverse of n = 12345678999999999987654321
Palindrome = Yes

Time Complexity: O(log10(n)) where n is the input number.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :