Java Program to Reverse a Number & Check if it is a Palindrome

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 notclass 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");    }}
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
Take input in BigInteger variable.Reverse the given BigInteger using the reverse method.Compare both BigIntegers using compareTo() method.
Take input in BigInteger variable.
Reverse the given BigInteger using the reverse method.
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 notimport 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");    }}
Reverse of n = 12345678999999999987654321
Palindrome = Yes


Time Complexity: O(log10(n)) where n is the input number.
palindrome
Reverse
Java
Java Programs
Java
palindrome
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Implementing a Linked List in Java using Class
Factory method design pattern in Java