Open In App

Palindrome Number Program in Java

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A given number can be said to be palindromic if the reverse of the given number is the same as that of a given number. In this article, we will write a Program to check if a number is a Palindrome Number in Java.

Example of Palindrome Number:

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

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

nput : n = 1
Output: Reverse of n = 1
Palindrome : Yes

Methods to Check If a Number Is a Palindrome Number in Java

There are certain methods to check if a Number is a Palindrome Number in Java as mentioned below:

  1. Using Iterative Approach
  2. Using Recursive Approach

1. Check for Palindrome Number using Iteration

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

Algorithm:

Iterative Algorithm when the given input is an integer number.

  • Initialize reversed_number = 0
  • Loop while number > 0
    • Multiply reversed_number by 10 and add number % 10 to reversed_number
      • reversed_number = reversed_number*10 + number %10;
      • Divide the number by 10
  • Return reversed_number

Program to Check for Palindrome Numbers in Java

Example:

Java
// Java program to reverse a number
// and find if it is a palindrome or not

// Driver Class
class Geeks
{
    // 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

The complexity of the above method:

Time Complexity: O(log10(n)) where n is the input number.
Auxiliary space: O(1) because constant variables have been used

2. Check for Palindrome Numbers using Recursion

The idea is to solve this problem by using recursion in a different way, Below are the steps:

  • Define a recursive method recursive_func().
  • It should have two parameters as the given number N and the resultant reverse number as rev.
  • Recursively reversing the number until N becomes less than 10 (base condition).
  • Finally returning the reversed number.

Example:

Java
// Java program to reverse a number
// and find if it is a palindrome or not

// Driver Class
class Geeks
{
    // Recursive function to reverse
    // the digits of number
    static int recursive_func(int n, int rev)
    {
        if (n < 10) {
            return rev * 10 + n;
        }
        else {
            int last_digit = n % 10;
            rev = rev * 10 + last_digit;
            return recursive_func(n / 10, rev);
        }
    }

    // Driver Code
    public static void main(String[] args)
    {
        int n = 123464321;
        int rev = recursive_func(n, 0);

        System.out.println("Reverse of n = " + rev);

        // Checking if n is same
        // as reverse of n
        if (n == rev)
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}

Output
Reverse of n = 123464321
Palindrome = Yes

The complexity of the above method:

Time Complexity: O(log10N)
Space Complexity: O(n) n-no of digits in a number [stack space]

Palindrome BigInteger Number

Big Integer can’t be operated using the same as the normal Integer so we need to use BigInteger Class to solve the issue faced like Overflowing of the values. The length of the number is log10(n), i.e. For BigIntegers using string operations like creation reverse and check palindrome will take log10(n) time. 

Approach to Check Palindrome for BigInteger:

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

Example: Java program to check if a BigInteger is Palindrome or not.

Java
// Java program to reverse a number
// and find if it is a palindrome or not
import java.io.*;
import java.math.BigInteger;

// Driver Class
class Geeks {
    // 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

Below is the complexity of the method mentioned above:

Time Complexity: O(log10(n)) where n is the input number.
Auxiliary Space: O(n) for StringBuilder

String Palindrome Number

Although the point is not fully justified as the string number is itself a string so It can be said to check is a string is palindrome or not ,and the method to check so is mentioned below.

Using In-built Methods

We can convert a number into String, and after conversion, we can use the functions available with Strings for conversions.

Example: Java program to check if a number is a palindrome using String.

Java
// Java Program to Check if a Number
// is Palindrome using String Class
import java.io.*;
import java.util.*;

// Driver Class
class Geeks {
    // main function
    public static void main(String[] args)
    {
        String s = "12345654321";

        // Length of the String
        int n = s.length();
        String rev = "";

        for (int i = n - 1; i >= 0; i--) {
            rev = rev + s.charAt(i);
        }

        // Printing the reversed Number
        System.out.println("Reverse Number = " + rev);

        // Checking Palindrome
        if (s.equals(rev))
            System.out.println("Palindrome = Yes");
        else
            System.out.println("Palindrome = No");
    }
}

Output
Reverse Number = 12345654321
Palindrome = Yes


Two Pointer Apporach

This is the optimal approach to reverse a number if it is given as a string. We can convert it into the character array and then match the first character with the last character.

Algorithm:

Iterative Algorithm when the given input is an integer number.

  • Initialize a left and right pointer, left with 0th index and right with last index
  • Loop while left < right
    • Check if arr[left]==arr[right]
      • If it doesn’t match then return false.
      • Otherwise increase left = left+1 and right = right-1.
  • If the loop completed so the number is palindrome.


Example: Two pointer approach to check if a number is a palindrome.

Java
// Java program to check if a number is a palindrome
// using two pointers if it is a string
public class Geeks
 {
    public static boolean isPalindrome(String s) {

        // Convert the string to char array for easier access
        char[] arr = s.toCharArray();

        // Two pointers: one at the start
        // and one at the end
        int l = 0, r = arr.length - 1;

        // Compare characters from both ends moving toward the center
        while (l < r) {
            if (arr[l] != arr[r]) {
                return false;  
            }
            // Move left pointer to the right
            l++; 
            // Move right pointer to the left  
            r--;  
        }

        return true;  // Return true if all characters match
    }

    public static void main(String[] args) {
        String s = "12345654321";
        
        // Checking if the string is a palindrome
        if (isPalindrome(s)) {
            System.out.println(s + " is a palindrome.");
        } else {
            System.out.println(s + " is not a palindrome.");
        }
    }
}

Output
12345654321 is a palindrome.

Time Complexity: O(log10(n)) where, n is the input number.
Auxiliary Space: O(1)

Note: Negative numbers (e.g., -121) are not considered palindromes since the ‘-‘ sign cannot match at both ends.

For more information, please refer to the complete article for Palindrome Number Program.



Next Article
Article Tags :
Practice Tags :

Similar Reads