Palindrome Program in Java | Java Program To Check Polindrome


Java has gained a whole lot of limelight amongst programmers due to its simplicity, structure neutrality, platform independence, distributed execution, and reusability. Additionally, java has many predefined libraries for seamless programming. And everybody can be enthusiastic to code in such a language.

Now coming to our subject, we’re going to write a java code to verify if the given enter is a palindrome or not.

A quantity or phrase is alleged to be a palindrome if it stays the identical after reversing it. And we will implement an answer iteratively in addition to recursively. So let’s get began!

Iterative Java Code

The thought is easy; we’ll declare a variable ‘reverse’ which shops the reversed variety of our enter.

We’ll multiply the reverse variable by 10(to fill the unit’s place with 0) in each iteration. Then, we are going to add the rest of the enter quantity after dividing it by 10. After including the remainder, we are going to divide the enter quantity by 10 (to take away the quantity within the unit’s place).

We’ll cease the above algorithm when the enter quantity turns into 0, and the quantity current within the reverse variable would be the reverse of the enter quantity.

public class upGrad{

public static void predominant(String[] args) {

int n=12221;

int reverse=0;

int temp=n;







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


System.out.print(n+” just isn’t a palindrome”);



Within the above code, we’ve declared a variable ‘n’ which shops the preliminary quantity, and we’ve to verify if the quantity n is a palindrome or not. Within the whereas loop, we’ll comply with the algorithm which we’ve mentioned earlier. And eventually, we’re checking if the reversed quantity is the same as the preliminary quantity or not. If the modified quantity and the preliminary numbers are related, we’re printing it as a palindrome else, not a palindrome.

MORE:   Best Home Remedies: From Tulsi to Ginger To Cardamom, Take These Ancient Herbs To Bid Adieu To Monsoon-Induced Flu

Now, this code will work just for an integer enter. And if we need to verify if a given phrase is a palindrome or not, we’ve to take care of it utilizing strings. And right here’s the code to do this.

public class upGrad{

public static void predominant(String[] args) {

String s=”rotor”;

String reverse=new String();

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



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


System.out.print(s+” just isn’t a palindrome”);



Within the above code, we’re appending the preliminary string’s characters from the tail to the pinnacle to the reverse string and checking if it is the same as the preliminary string or not. Now we have hardcoded the string with a phrase, however we will initialize it with consumer enter utilizing the scanner class.

Recursive Java Code

On this implementation, we’re going to examine the primary and final characters. And if they’re equal, then recur additional for the remaining string.

However this logic won’t work for the strings which have an odd variety of characters. So if we embrace a base case, the place we conclude a string as a palindrome, if the size of a string is one, i.e., the primary and final characters’ place is similar. This may clear our problem with odd-sized strings as a result of we’ll recur to the center factor after which conclude it as a palindrome since solely a single character stays within the center.

public class upGrad{

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


            return true;


            return false;

MORE:   It’s A Glamorous Sunday For Sophie Choudry! Singer’s Latest Insta Video Post Is Too Hot To Handle


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

        return true;


public static void predominant(String[] args) {

String s1=”rotor”;

String s2=”programming”;





Within the above code, we’ve got written a operate that expects a string, two integers because the parameters. And the 2 integers, low, excessive are the pointers that maintain monitor of the characters which must be checked. And if the names on the place high and low are equal.

we’ll name the identical operate with up to date parameters such that the string is shrunk from each side by one character. And if the pointers high and low meet one another or a single character is current between them, then we’ve reached until the center 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 excessive is 4. Because the character at 0th place (‘r’) is the same as the character at 4th place (‘r’), we’ll make a recursive name with low up to date as low+1 and excessive up to date as high-1.

Now, low is 1, and excessive is 3 since characters at these positions are equal, we’ll once more make a recursive name. Now low is 2 and excessive is 2, and it triggers the bottom case the place low is equal to excessive, so we’ll return true.

We will additionally implement a recursive operate to verify if an integer is a palindrome or not, and right here’s the method to do this.

MORE:   Kate Bosworth Birthday: From Satin Robes to Floral Prints – 5 Outfits That Show Why She Is Our Model Muse

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


            return n==rev;


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


Word that, within the above operate, initially n and temp are the identical. As a result of eventually, we’ve to match the reverse quantity with the preliminary quantity, so all of the computations are carried out on the identical variable. The preliminary quantity shouldn’t be altered.

Additionally Learn: Java Challenge Concepts & Subjects


We’ve understood the definition of palindrome, walked via iterative and recursive codes for checking if a string/quantity is a palindrome or not. We all know the code by a dry run of pattern instance. Now that you’re conscious of implementing a code to verify palindrome attempt implementing it utilizing the scanner class and check out coding it utilizing OOP ideas.

In the event you want to enhance your Java abilities, you should get your fingers on these java tasks. In the event you’re to study extra about Java, full-stack growth, take a look at upGrad & IIIT-B’s PG Diploma in Full-stack Software program Improvement, which is designed for working professionals and provides 500+ hours of rigorous coaching, 9+ tasks, and assignments, IIIT-B Alumni standing, sensible hands-on capstone tasks & job help with high corporations.

Develop into a Full Stack Developer



Socially Keeda

Socially Keeda, the pioneer of news sources in India operates under the philosophy of keeping its readers informed. tells the story of India and it offers fresh, compelling content that’s useful and informative for its readers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker