Palindrome Number Program in Java
Last Updated :
08 Apr, 2025
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:
- Using Iterative Approach
- 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");
}
}
OutputReverse 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");
}
}
OutputReverse 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");
}
}
OutputReverse 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");
}
}
OutputReverse 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.");
}
}
}
Output12345654321 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.
Similar Reads
Reverse Number Program in Java In Java, reversing a number means that the digit at the first position should be swapped with the last digit, the second digit will be swapped with the second last digit, and so on, till the middle element.Example of reversing a number:Input: n = 357Output: 753Input n = 100Output: 1 ( leading zeros
3 min read
Java Program to Check Whether a String is a Palindrome A string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look
8 min read
Java Program to Find All Palindromic Sub-Strings of a String Given a string, the task is to count all palindrome substring in a given string. Input : aba Output : 4 Explanation : All palindrome substring are : "aba" , "a" , "b", "a" Input : TENET Output : 7 Explanation : All palindrome sub-string are : "T" , "E" , "N", "E", "T" , "ENE" , "TENET" Approach: Tak
2 min read
Java Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco
2 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Exercises - Basic to Advanced Java Practice Programs with Solutions Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers
7 min read