Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Java Program to Check Whether a Number is a Palindrome or Not

Last Updated on July 24, 2023 by Mayank Dham

A palindrome is a word, phrase, number, or sequence of characters that reads the same backward as it does forward. In other words, it remains unchanged when its characters are reversed. Palindromes can be found in various contexts, including words, sentences, and numbers. Finding Palindrome number in Java is one of the most interesting things. A number is given as the input, we have to find whether the number is palindrome number in Java.

## What are Palindrome Numbers in Java?

A palindrome number is a number that is equal to its reverse number. Basically, palindrome number is a number that has the same digits while reading it from the backward as well as forward. We considered some examples below to understand what is a palindrome number.

``````Given Number      =  2112
Reverse Number  =  2112
Thus, the given number (2112) and the reverse number (2112) are the same so the number 2112 is a palindrome

Given Number      =  1234
Reverse Number  =  4321
Here, the given number (1234) and the reverse number (4321) are not the same so the number 1234 is not a palindrome``````

## Algorithm to Check whether a Number is Palindrome or not

Below are the major steps for the algorithm.

• Initialize two variables: originalNum and reversedNum to store the original number and the reversed number, respectively.
• Set originalNum to the input number that needs to be checked for palindrome.
• Set reversedNum to 0.
• Repeat the following steps until originalNum becomes 0:
a. Get the last digit of originalNum using the modulo operator (num % 10).
b Append the last digit to reversedNum by multiplying reversedNum by 10 and adding the last digit (reversedNum = reversedNum * 10 + digit).
c. Remove the last digit from originalNum by dividing it by 10 (num = num / 10).
• After the loop, compare originalNum with reversedNum.
a. If they are equal, the number is a palindrome. Return true.
b. If they are not equal, the number is not a palindrome. Return false

### Let’s understand the algorithm with help of an example and a dry run.

``Given Number N= 1441``

Step 1:- Initialize Reverse Number reverse_number=0
Step 2:- Reverse a given number and store it in reverse_number. The logic to reverse a number is given below.

• Run a while loop until N becomes 0.
• Get last digit of N (N%10) and add it into reverse_number (reverse_number*10+last digit)
• Remove the last digit of N (N/10)

N=1441 reverse_number=0

N (1441) > 0:
last _digit = N%10 = 1441%10 = 1

reverse_number= reverse_number10+last_digit = 010+1 = 1

N= N/10 = 1441/10 = 144

N (144) > 0:
last _digit = N%10 = 144%10 = 4

reverse_number= reverse_number10+last_digit = 110+4 = 14

N= N/10 = 144/10 = 14

N (14) > 0:
last _digit = N%10 = 14%10 = 4

reverse_number= reverse_number10+last_digit = 1410+4 = 144

N= N/10 = 14/10 = 1

N (1) > 0:
last _digit = N%10 = 1%10 = 1

reverse_number= reverse_number10+last_digit = 14410+1 = 1441

N= N/10 = 1/10 = 0

N (0) > 0 False:
break while loop.

Now, we got a reverse of a given number.

Step 3:- Now, compare the given number and the reverse number. Here the given number N=1441 and the reverse of the given number reverse_number=1441 both are the same thus our answer is true.

## Palindrome Program in Java:-

Let’s discuss an approach to solving the palindrome program in java. First, we will discuss palindrome in java using reverse numbers.

### Method to Check Palindrome in Java using Reverse Number

```//palindrome program in java
class Prepbytes{
// method to reverse number
public static int reverse_number(int n){
int reverse_n=0;
while(n>0){
reverse_n=reverse_n*10+n%10;
n=n/10;
}
return reverse_n;
}

// method to check palindrome in java
public static boolean is_palindrome(int n){
// first we will find the reverse number
int reverse_n=reverse_number(n);
// if the reverse number and the given number are the same then the number is a palindrome
if(reverse_n==n){
return true;
}
else{
return false;
}
}

public static void main(String[] args) {
int n=8118;
if(is_palindrome(n)==true){
System.out.println("Number N: "+n+" is palindrome a number");
}
else{
System.out.println("Number N: "+n+" is not a palindrome number");
}
int n1=1234;
if(is_palindrome(n1)==true){
System.out.println("Number N: "+n1+" is palindrome a number");
}
else{
System.out.println("Number N: "+n1+" is not a palindrome number");
}
}
}
```
``````Output:-
Number N: 8118 is a palindrome number
Number N: 1234 is not a palindrome number``````

In the above program to check palindrome in java first, we have created a method to find a reverse number of a given number in which we run a while loop until the given number is greater than 0 after that we will reverse number * 10, and n%10 (last digit) into reverse number. After that, we will divide n by 10 and run a loop until n is greater than zero. We have also created a method to check given number is a palindrome in java or not in that method we first call the reverse_number method to find a reverse number of a given number.

Time Complexity:- O(logn) we are using while loop to reverse the given number which will be executed log n times.

Space Complexity:- O(1) we are not using any extra data structure to store any data.

### Method to Check Palindrome in Java using String.

```// program to check palindrome in java
class Prepbytes{
// method to check palindrome in java
public static boolean is_palindrome(int n){
// convert the given number into a string
String str_n=Integer.toString(n);
int length=str_n.length();
// run loop till half of the length to check ith and length-i-1th char are the same
for(int i=0;i<length/2;i++){
// if the char is not the same then return false
if(str_n.charAt(i)!=str_n.charAt(length-i-1)){
return false;
}
}
return true;
}

public static void main(String[] args) {
int n=8118;
if(is_palindrome(n)==true){
System.out.println("Number N: "+n+" is palindrome a number");
}
else{
System.out.println("Number N: "+n+" is not a palindrome number");
}
int n1=1234;
if(is_palindrome(n1)==true){
System.out.println("Number N: "+n1+" is palindrome a number");
}
else{
System.out.println("Number N: "+n1+" is not a palindrome number");
}
}
}
```
``````Output:-
Number N: 8118 is palindrome a number
Number N: 1234 is not a palindrome number``````

In the above palindrome program in java, first, we created a method that converts a given number into a string and then we run for loop to see if the first and last, second and second last and so on characters are matching if it is not matching then we will return false else we will return true.

Time Complexity:- O(logn) we are running a loop from 0 to length of string/2 which is equal to log(n)/2 so we can say effective time complexity as log(n).

Space Complexity:- O(1) we are not using any extra data structure to store any data.

### Method to Finding Palindrome in java Using Recursion.

```// palindrome program in java
class Prepbytes{
// recursive function to find the reverse of a given number
public static int reverse_number(int n,int rev_num){
if(n==0){
return rev_num;
}
rev_num=rev_num*10+n%10;
return reverse_number(n/10,rev_num);
}

// a method to check if a given number is palindrome or not
public static boolean is_palindrome(int n){
int rev_num=reverse_number(n,0);
if(n==rev_num){
return true;
}
else{
return false;
}
}

public static void main(String[] args) {
int n=8118;
if(is_palindrome(n)==true){
System.out.println("Number N: "+n+" is palindrome a number");
}
else{
System.out.println("Number N: "+n+" is not a palindrome number");
}
int n1=1234;
if(is_palindrome(n1)==true){
System.out.println("Number N: "+n1+" is palindrome a number");
}
else{
System.out.println("Number N: "+n1+" is not a palindrome number");
}
}
}
```
``````Output:-
Number N: 8118 is palindrome a number
Number N: 1234 is not a palindrome number``````

In the above program to check palindrome in java, first, we created a recursive method to find the reverse number of the given number in which our base case if when n becomes zero at that time we return rev_num if it is not the base case then we add the last digit of the given number n at the end of the rev_num and call recursive method until we reach the base case. We also created a method to check whether a given number is a palindrome number or not in which we first find the reverse number of a given number and then we check if the reverse number and given number are the same if it is true then we return true else we return false.

Time Complexity:- O(logn) we making log(n) recursive calls to find the reverse number.

Space Complexity:- O(1) we are not using any extra data structure to store any data.

Conclusion
As we’ve witnessed, the palindrome number in Java is not only a captivating mathematical concept but also an excellent opportunity to showcase the elegance and power of Java programming. From simple iterative loops to more advanced custom functions, the solutions provided in this article serve as valuable tools in your programming arsenal.

Palindrome numbers in Java find applications in diverse fields, such as cryptography, data validation, and algorithmic challenges. Understanding and efficiently implementing palindrome detection algorithms equip Java developers with the essential skills to tackle real-world problems and build robust applications.

## FAQ (Frequently Asked Questions) on palindrome number in Java:

Here are a few FAQs on palindrome number in Java.

Q: Why are palindrome numbers interesting in computer science?
A: Palindrome numbers offer intriguing challenges for programmers and demonstrate the power of different programming techniques. They are commonly used in problem-solving tasks, data validation, and cryptography.

Q: How can I check if a number is a palindrome in Java?
A: There are several approaches to check if a number is a palindrome in Java. You can convert the number to a string and compare the characters, reverse the number and compare it with the original, or use recursion to compare digits from both ends.

Q: Are there other methods to check for palindromes?
A: Yes, apart from the approaches covered in the article, there are additional techniques to identify palindromes. For instance, you can use Java’s StringBuilder to reverse the number, or you can convert the number to an array and compare elements from both ends.

Q: How does the iterative approach compare to the recursive one in terms of efficiency?
A: The iterative approach is generally more efficient in terms of both time and space complexity, as it avoids the overhead of recursive calls. The recursive approach can be less efficient due to the potential for a large number of function calls for longer numbers.

Q: Can I use the concepts learned in this article for other programming languages?
A: Yes, the concepts and techniques demonstrated in this article are not limited to Java. You can apply similar approaches to detect palindrome numbers in other programming languages as well. Understanding the fundamentals is valuable across various programming paradigms.

Other Java Programs