Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A palindrome program in Java checks whether a given string or number reads the same forwards and backwards. Palindromes like "madam" or "121" are common examples. This program is widely used in Java coding interviews to test skills in string manipulation, loops, and conditional logic. Writing a palindrome program helps developers strengthen their understanding of Java strings, control flow, and basic algorithm design.
A palindrome number in Java is a numeric value that remains the same when its digits are reversed. In other words, a number is a palindrome if reading it from left to right is the same as reading it from right to left.
Example:
121, 1331, and 12321 are palindrome numbers.
123 is not a palindrome.
In Java, checking a palindrome number typically involves reversing the number using arithmetic operations and comparing it with the original value.
Problem Statement
The problem is that you are given a number, and you have to check whether the number is a palindrome number or not.
Sample Input1:
121121
Sample Output1:
The number 121121 is a Palindrome Number.
Sample Input2:
123421
Sample Output2:
The number 123421 is not a Palindrome Number.
Explanation
For the number 121121, you can see that if you read the number from backwards or forward, it reads the same. So this number is a palindrome.
But for the number 123421, it’s not a palindrome as it doesn’t read the same for the backwards and forward.
Solution
This problem can be solved by using different approaches. Let's see them one by one.
Approach1(Manually Reverse the Number)
Algorithm:
Take the number as an input.
Store it in a temporary variable.
Reverse the number.
Compare the reversed number with the temporary number.
Code:
public class PalindromeNumber {
public static void main(String[] args) {
int Number = 121131;
int Temp = Number;
int Reverse = 0,Rem;
while(Number > 0) {
Rem = Number % 10;
Reverse = Reverse * 10 + Rem;
Number /= 10;
}
if(Temp == Reverse) {
System.out.printf("The Number %d is a Palindrome Number",Temp);
}
else{
System.out.printf("The Number %d is not a Palindrome Number", Temp);
}
}
}
You can also try this code with Online Java Compiler
Let’s see how efficient this algorithm is by understanding its time and space complexity.
Time Complexity Let the number be n. As shown in the above program, we divide the number n by 10 in each iteration until the number n becomes 0. So total number of steps required = log10(n). Time complexity = O(log10(n))
Space Complexity We are only using three variables for this case: one to store the number, one to store the remainder, and one to reverse the number. So overall space complexity becomes O(1).
Approach2(Take the Number as String)
Algorithm:
The steps are as follows -
Take the number as a string.
Store it in a temporary variable.
Reverse the string.
Compare the reverse string with the temporary string.
Code:
public class PalindromeNumber {
public static void main(String[] args) {
String Number = "121131";
String Reverse = "";
int len = Number.length();
int i = len - 1;
while(i >= 0) {
Reverse = Reverse + Number.charAt(i);
i--;
}
if(Number.equals(Reverse)) {
System.out.printf("The Number " + Number + " is a Palindrome Number");
}
else{
System.out.printf("The Number " + Number + " is not a Palindrome Number");
}
}
}
You can also try this code with Online Java Compiler
Let’s see how efficient this algorithm is by understanding its time and space complexity.
Time Complexity Here, in this case, we are taking the Number as a string, reversing the string by traversing it from right to left. Time complexity = O(length of the string) Now, the length of the string = log10(n) So, Time complexity = O(log10(n))
Space Complexity Here also, we are not allocating extra space. So, Space Complexity = O(1)
Approach3(Take the Number as a String and use the Two Pointers Method)
Algorithm:
The steps are as follows -
Take the Number as a string.
Use two pointers where one points to the beginning of the string and the other one point to the end of the string.
Run a loop until the left pointer crosses the right pointer.
Compare the characters pointed by the pointers.
If they are the same, move the left pointer one step right and the right pointer one step left.
If they are not the same, the number is not a palindrome and return from the loop.
Code:
public class PalindromeNumber {
public static void main(String[] args) {
String Number = "121131";
int left = 0;
int right = Number.length() - 1;
while(left < right) {
if(Number.charAt(left) != Number.charAt(right)) {
System.out.println("The Number "+ Number +" is not a palindrome");
return;
}
left++;
right--;
}
System.out.println("The Number "+ Number+" is a Palindrome");
}
}
You can also try this code with Online Java Compiler
Let’s see how efficient this algorithm is by understanding its time and space complexity.
Time Complexity
Here also, We are traversing the length of the string, but here the number of steps at max would be (length of the string) / 2. So the time complexity here is O(length of string / 2). Now, length of the string = log10(n) So, Time complexity = O(log10(n))
Space Complexity Here also we are not using any extra spaces. So, Space complexity = O(1)
Palindrome in Java: BigInteger, String, and Two-Pointer Approaches
Checking whether a number is a palindrome in Java is a common coding problem, especially when working with very large numbers. Normal integer operations fail for extremely large values due to overflow, so we use BigInteger, string methods, or an optimal two-pointer technique.
1. Palindrome Check for BigInteger in Java
A BigInteger cannot be processed like a normal int or long, as it may exceed the range of primitive data types. To avoid overflow, we use the BigInteger class and perform string-based operations for reversing and comparison.
Why BigInteger?
Supports values larger than primitive data types
Prevents overflow errors
Allows string-based manipulation
Steps to Check BigInteger Palindrome
Take input as a BigInteger.
Convert it to a String.
Reverse the string using StringBuilder.
Convert the reversed string back to BigInteger.
Compare both using compareTo().
Java Program: Check BigInteger Palindrome
import java.math.BigInteger;
class codingninjas {
// Method to reverse a BigInteger
public static BigInteger reverse(BigInteger n) {
String s = n.toString();
StringBuilder sb = new StringBuilder(s);
return new BigInteger(sb.reverse().toString());
}
public static void main(String[] args) {
BigInteger n = new BigInteger("12345678999999999987654321");
BigInteger reverseN = reverse(n);
System.out.println("Reverse of n = " + reverseN);
if (n.compareTo(reverseN) == 0)
System.out.println("Palindrome = Yes");
else
System.out.println("Palindrome = No");
}
}
Output:
Reverse of n = 12345678999999999987654321
Palindrome = Yes
Complexity:
Operation
Operation
Complexity
Time
O(log₁₀(n))
Space
O(n) due to StringBuilder
2. Palindrome Check Using String in Java
When the number is already in string format, checking if it is a palindrome becomes straightforward.
Steps:
Convert the number to a string (if required)
Reverse the string manually or using built-in methods
Compare original and reversed strings
Java Program: Palindrome Using String:
class Codingninjas {
public static void main(String[] args) {
String s = "12345654321";
int n = s.length();
String rev = "";
for (int i = n - 1; i >= 0; i--) {
rev = rev + s.charAt(i);
}
System.out.println("Reverse Number = " + rev);
if (s.equals(rev))
System.out.println("Palindrome = Yes");
else
System.out.println("Palindrome = No");
}
}
Output:
Reverse Number = 12345654321
Palindrome = Yes
3. Two-Pointer Approach (Optimal Method)
The two-pointer approach is the most efficient method to check a palindrome when the input is a string.
Algorithm:
Convert the string to a char array.
Initialize two pointers: left = 0 , right = last index
Loop while left < right:
If characters don’t match → Not a palindrome
Move pointers inward
If the loop completes → Palindrome
Java Program: Palindrome Check Using Two Pointers:
public class Codingninjas {
public static boolean isPalindrome(String s) {
char[] arr = s.toCharArray();
int l = 0, r = arr.length - 1;
while (l < r) {
if (arr[l] != arr[r]) {
return false;
}
l++;
r--;
}
return true;
}
public static void main(String[] args) {
String s = "12345654321";
if (isPalindrome(s)) {
System.out.println(s + " is a palindrome.");
} else {
System.out.println(s + " is not a palindrome.");
}
}
}
Output:
12345654321 is a palindrome.
Complexity
Operation
Complexity
Time
O(log₁₀(n))
Space
O(1)
Final Summary:
BigInteger palindrome check uses string reversal and compareTo().
String-based palindrome check is simple and beginner-friendly.
Two-pointer approach is the most optimal for string inputs.
All methods handle numeric palindrome logic efficiently.
Frequently Asked Questions
Is 1221 a palindrome?
Yes, 1221 is a palindrome because it reads the same forward and backward. Reversing 1221 results in 1221, confirming it's a palindrome.
What are the examples of palindrome numbers?
Some examples of the palindrome numbers are 121, 33233 etc.
How does the Palindrome Program work?
The palindrome program checks whether a number is a palindrome or not by reversing the number and checking for equality.
Which data structure can be used for Palindrome Program?
In the palindrome program, for a number of length n, we compare the ith digit with (n - i - 1)th digit. If it is found the same, we move forward. So in that scenario, the Deque data structure would be suitable.
Conclusion
The palindrome program in Java is a fundamental exercise that strengthens understanding of string and number manipulation, control flow, and logic building. Whether checking strings or numbers, it reinforces key Java concepts such as loops, conditionals, and method design.