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
Prime Number Program in Java
A prime number is a natural number greater than 1, divisible only by 1 and itself. Examples include 2, 3, 5, 7, and 11. These numbers have no other factors besides themselves and one. In this article, we will learn how to write a prime number program in Java when the input given is a Positive number
6 min read
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: 753 Input n = 100Output: 1 ( leading zer
3 min read
Java Program for Longest Palindromic Subsequence | DP-12
Given a sequence, find the length of the longest palindromic subsequence in it. As another example, if the given sequence is "BBABCBCAB", then the output should be 7 as "BABCBAB" is the longest palindromic subsequence in it. "BBBBB" and "BBCBB" are also palindromic subsequences of the given sequence
3 min read
Java Program to check Armstrong Number
Given a number x. Write a Java Program to determine whether the given number is Armstrong's number or not. In this article, we will learn how to check Armstrong Numbers in Java. What is Armstrong's Number?In a mathematical number system, the Armstrong number is the number in any given number base, w
4 min read
How to Find Prime and Palindrome Numbers using Multi-Threading in Java?
Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other. Multiple threads don't
4 min read
Java Program For Checking Linked List With A Loop Is Palindrome Or Not
Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop. Examples: Input: 1 -> 2 -> 3 -> 2 /| |/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input: 1 -> 2 -> 3 -> 4 /| |/ ------- 1
4 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
TCS Coding Practice Question | Check Palindrome Number
Given a number, the task is to check if this number is Palindrome or not using Command Line Arguments . Examples: Input: 123Output: NoInput: 585Output: YesApproach: Since the number is entered as Command line Argument, there is no need for a dedicated input lineExtract the input number from the comm
4 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 Rotate digits of a given number by K
Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read