1.
Introduction
2.
Problem Statement
2.1.
Sample Input1:
2.2.
Sample Output1:
2.3.
Sample Input2:
2.4.
Sample Output2:
3.
Explanation
4.
Solution
4.1.
Approach1(Manually Reverse the Number)
4.1.1.
Algorithm:
4.1.2.
Code:
4.1.3.
Output:
4.1.4.
4.1.5.
Must Read  C Program to Reverse a Number
4.1.6.
Program Flow Diagram:
4.1.7.
Complexity Analysis
4.2.
Approach2(Take the Number as String)
4.2.1.
Algorithm:
4.2.2.
Code:
4.2.3.
Output:
4.2.4.
Program Flow Diagram:
4.2.5.
Complexity Analysis
4.3.
Approach3(Take the Number as a String and use the Two Pointers Method)
4.3.1.
Algorithm:
4.3.2.
Code:
4.3.3.
Output:
4.3.4.
Program Flow Diagram:
4.3.5.
Complexity Analysis
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

# Palindrome Program in Java

Aniket Majhi
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

## Introduction

The term palindrome means any word, sentence, phrase or number that reads the same backwards or forward. Here in this blog, you will learn the concept of palindrome, and you will know the steps involved to find out whether a number is a palindrome using different approaches. At the end of this blog, every concept regarding the Palindrome Program will be clear to you.
So, without further ado, let’s get started.

## 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Solution

This problem can be solved by using different approaches. Let's see them one by one.

### Approach1(Manually Reverse the Number)

#### Algorithm:

1. Take the number as an input.
2. Store it in a temporary variable.
3. Reverse the number.
4. 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);
}
}
}``````

#### Output:

Practice it on online java compiler for better understanding.

#### Complexity Analysis

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 -

1. Take the number as a string.
2. Store it in a temporary variable.
3. Reverse the string.
4. 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");
}
}
}``````

#### Complexity Analysis

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 -

1. Take the Number as a string.
2. Use two pointers where one points to the beginning of the string and the other one point to the end of the string.
3. Run a loop until the left pointer crosses the right pointer.
4. Compare the characters pointed by the pointers.
5. If they are the same, move the left pointer one step right and the right pointer one step left.
6. 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");
}
}``````

#### Output:

Also check out Addition of Two Numbers in Java here.

#### Complexity Analysis

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)

Check out this problem - Check If A String Is Palindrome

Must Read What are Loops in Java.

## FAQs

1. What is a palindrome number?
A palindrome number is a number that reads the same if we reverse the number.

2. What are the examples of palindrome numbers?
Some examples of the palindrome numbers are 121, 33233 etc.

3. 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.

4. 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.

## Key Takeaways

In this article, we have extensively discussed the Palindrome Program topic and its implementation in Java.

Here in this blog, we started with the basic introduction. We discussed the problem statement with an example. Then, we discussed different approaches to solve the problem and their implementations in Java with proper explanation using the program flow diagram.

We hope that this blog has helped you enhance your knowledge regarding the Palindrome Program and if you would like to learn more, check out our articles on Array Programs in Java Maximum of All Subarrays of Size K. Do upvote our blog to help other ninjas grow.