# Palindrome Program in Java | Java Program To Check Polindrome

## Introduction

Java has gained a lot of limelight among programmers because of its simplicity, architecture neutrality, platform independence, distributed execution, and reusability. Also, java has many predefined libraries for seamless programming. And everyone will be enthusiastic to code in such a language.

Now coming to our topic, we are going to write a java code to check if the given input is a palindrome or not.

A number or word is said to be a palindrome if it remains the same after reversing it. And we can implement a solution iteratively as well as recursively. So let’s get started!

## Iterative Java Code

The idea is simple; we’ll declare a variable ‘reverse’ which stores the reversed number of our input.

We will multiply the reverse variable by 10(to fill the unit’s position with 0) in every iteration. Then, we will add the remainder of the input number after dividing it by 10. After adding the rest, we will divide the input number by 10 (to remove the number in the unit’s position).

We’ll stop the above algorithm when the input number becomes 0, and the number present in the reverse variable will be the reverse of the input number.

public static void main(String[] args) {

int n=12221;

int reverse=0;

int temp=n;

while(temp>0){

reverse=reverse*10;

reverse=reverse+temp%10;

temp=temp/10;

}

if(reverse==n)

System.out.print(n+” is a palindrome”);

else

System.out.print(n+” is not a palindrome”);

}

}

In the above code, we’ve declared a variable ‘n’ which stores the initial number, and we’ve to check if the number n is a palindrome or not. In the while loop, we’ll follow the algorithm which we’ve discussed earlier. And finally, we are checking if the reversed number is equal to the initial number or not. If the changed number and the initial numbers are similar, we are printing it as a palindrome else, not a palindrome.

Now, this code will work only for an integer input. And if we want to check if a given word is a palindrome or not, we’ve to deal with it using strings. And here’s the code to do that.

public static void main(String[] args) {

String s=”rotor”;

String reverse=new String();

for(int i=s.length()-1;i>=0;i–)

reverse=reverse+s.charAt(i);

if(s.equals(reverse))

System.out.print(s+” is a palindrome”);

else

System.out.print(s+” is not a palindrome”);

}

}

In the above code, we are appending the initial string’s characters from the tail to the head to the reverse string and checking if it is equal to the initial string or not. We have hardcoded the string with a word, but we can initialize it with user input using the scanner class.

## Recursive Java Code

In this implementation, we are going to compare the first and last characters. And if they are equal, then recur further for the remaining string.

But this logic will not work for the strings which have an odd number of characters. So if we include a base case, where we conclude a string as a palindrome, if the length of a string is one, i.e., the first and last characters’ position is the same. This would clear our issue with odd-sized strings because we’ll recur to the middle element and then conclude it as a palindrome since only a single character remains in the middle.

public static boolean isPalindrome(String str, int low, int high){

if(low==high)

return true;

if(str.charAt(low)!=str.charAt(high))

return false;

if(high-low>1)

return isPalindrome(str,low+1,high-1);

return true;

}

public static void main(String[] args) {

String s1=”rotor”;

String s2=”programming”;

System.out.println(isPalindrome(s1,0,s1.length()-1));

System.out.println(isPalindrome(s2,0,s2.length()-1));

}

}

In the above code, we have written a function that expects a string, two integers as the parameters. And the two integers, low, high are the pointers that keep track of the characters which have to be checked. And if the names at the position low and high are equal.

we’ll call the same function with updated parameters such that the string is shrunk from both sides by one character. And if the pointers low and high meet each other or a single character is present between them, then we’ve reached till the middle of the string and concluding it as a palindrome.

Now, let’s have a dry run of the code for the string “rotor”. Initially, the low is 0, and the high is 4. Since the character at 0th position (‘r’) is equal to the character at 4th position (‘r’), we’ll make a recursive call with low updated as low+1 and high updated as high-1.

Now, low is 1, and high is 3 since characters at those positions are equal, we’ll again make a recursive call. Now low is 2 and high is 2, and it triggers the base case where low is equivalent to high, so we’ll return true.

We can also implement a recursive function to check if an integer is a palindrome or not, and here’s the process to do that.

static boolean isPalindrome(int n, int rev, int temp){

if(temp==0)

return n==rev;

rev=rev*10;

return isPalindrome(n,rev+temp%10,temp/10);

}

Note that, in the above function, initially n and temp are the same. Because at last, we’ve to compare the reverse number with the initial number, so all the computations are performed on the same variable. The initial number should not be altered.

Also Read: Java Project Ideas & Topics

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

## Conclusion

We’ve understood the definition of palindrome, walked through iterative and recursive codes for checking if a string/number is a palindrome or not. We know the code by a dry run of sample example. Now that you are aware of implementing a code to check palindrome try implementing it using the scanner class and try coding it using OOP concepts.

If you wish to improve your Java skills, you need to get your hands on these java projects. If you’re interested to learn more about Java, full-stack development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development, which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.