In this program, we’ll learn how to check whether a string or number is a palindrome in Java. In Java, a palindrome is a word, phrase, number, or sequence of characters that reads the same backward and forward.
This blog will show you how to check for a palindrome in Java with the help of an easy-to-follow program. Let’s first understand the concept of palindromes before diving into how to code them in Java!
Post Type: | Java Programs For Beginners |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
In this program, we will learn about the following concepts:
- What is Palindrome?
- What is a Palindrome number?
- What is a Palindrome string?
- What is a Palindrome phrase?
- Palindrome Program in Java using while loops
- Palindrome Program in Java using for loops
- Palindrome Program in Java using recursion
- Palindrome Program in Java using library method
What is Palindrome?
A palindrome is a word or phrase that can be spelled the same forwards and backward, ignoring spacing, punctuation, and capitalization.
A Palindrome number is the coolest kind of number – it remains the same even after reversing! For example, 38983. It’s also a string or sequence of characters, which means it has the same sequence of letters when reading forward and backward directions.
What is a Palindrome number?
A number is a palindrome if its digits are reversed and it remains the same! Here are some examples: 121, 737,34543.
Palindrome Number Program in Java Using While Loops
This program allows the user to enter ANY positive number. Next, it checks whether the given number is a palindrome using While Loop.
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx1 { public static void main(String[] args) { int originalNumber, temp, finalNumber=0, reminder; Scanner sc=new Scanner(System.in); System.out.println("Enter the number:- "); originalNumber=sc.nextInt(); // Assigning the value to temp Variable temp=originalNumber; while (originalNumber>0) { reminder=originalNumber%10; originalNumber=originalNumber/10; finalNumber=finalNumber*10+reminder; } // Two Value Comparison if (temp==finalNumber) { System.out.println(temp+ " is a Palindrome number"); } else System.out.println(temp+ " is Not a Palindrome number"); } }
Output:
Enter the number:- 125 125 is Not a Palindrome number
Enter the number:- 121 121 is a Palindrome number
Palindrome Number Program in Java Using For Loops
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx2 { public static void main(String[] args) { int originalNumber, temp, finalNumber=0, reminder; Scanner sc=new Scanner(System.in); System.out.println("Enter the number: "); originalNumber=sc.nextInt(); // Assigning the value to temp Variable temp=originalNumber; for( ;originalNumber != 0; originalNumber /= 10 ) { reminder = originalNumber % 10; finalNumber= finalNumber* 10 + reminder; } // Two Value Comparison if (temp==finalNumber) { System.out.println(temp+ " is a Palindrome"); } else System.out.println(temp+ " is Not a Palindrome"); } }
Output:
Enter the number: 153 153 is Not a Palindrome
Enter the number: 131 131 is a Palindrome
Palindrome Number Program in Java Using Recursion
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx3 { public static void main(String[] args) { int originalNumber, temp, finalNumber=0; Scanner sc=new Scanner(System.in); System.out.println("Enter the number: "); originalNumber=sc.nextInt(); temp=originalNumber; // Two Value Comparison if (getReverse(originalNumber, finalNumber)==temp) { System.out.println(temp+ " is a Palindrome"); } else System.out.println(temp+ " is Not a Palindrome"); } private static int getReverse(int originalNumber, int finalNumber) { if(originalNumber==0) return finalNumber; int reminder = originalNumber%10; finalNumber=finalNumber*10+reminder; return getReverse(originalNumber/10, finalNumber); } }
Output:
Enter the number: 789 789 is Not a Palindrome
Enter the number: 353 353 is a Palindrome
What is a Palindrome string?
A string is called a Palindrome String if you read it in any direction, forward or backward, and it remains the same.
Examples of Palindrome strings are:
- tenet
- rotor
- madam
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx4 { public static void main(String[] args) { String originalString, finalString=""; int length; Scanner sc=new Scanner(System.in); System.out.println("Enter a String: "); originalString=sc.nextLine(); length=originalString.length(); for(int i=length-1; i>=0; i--) { finalString=finalString+originalString.charAt(i); } // Two Value Comparison if (originalString.equalsIgnoreCase(finalString)) { System.out.println(originalString+ " is a Palindrome"); } else System.out.println(originalString+ " is Not a Palindrome"); } }
Output:
Enter a String: madam madam is a Palindrome
Enter a String: Softwaretestingo Softwaretestingo is Not a Palindrome
What is a Palindrome Phrase?
A palindrome is a sequence of characters that can be read the same way from both directions. Punctuation, capital letters, and spaces are usually ignored when creating palindromes.
Advanced Methods
There are few other advanced methods to find the Palindrome in numbers or strings. Here are the methods:
- Using StringBuilder
- Using for Loop and charAt
- Using toCharArray
- Using Stack
Find Palindrome Using StringBuilder
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx5 { public static void main(String[] args) { String originalString; Scanner sc=new Scanner(System.in); System.out.println("Enter a String: "); originalString=sc.nextLine(); if (isPalindrome(originalString.toLowerCase())) System.out.println(originalString+ " Is a Palindrome"); else System.out.println(originalString+ " Is Not a Palindrome"); } private static boolean isPalindrome(String string) { String reversed = new StringBuilder(string).reverse().toString(); return string.equals(reversed); } }
Enter a String: SoftwareTestingo SoftwareTestingo Is Not a Palindrome
Enter a String: madam madam Is a Palindrome
Find Palindrome Using for Loop and charAt
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx6 { public static void main(String[] args) { String originalString; Scanner sc=new Scanner(System.in); System.out.println("Enter a String or Number: "); originalString=sc.nextLine(); if (isPalindrome(originalString.toLowerCase())) System.out.println(originalString+ " Is a Palindrome"); else System.out.println(originalString+ " Is Not a Palindrome"); } private static boolean isPalindrome(String original) { String reversed = ""; int len = original.length(); for (int i = len - 1; i >= 0; i--) { reversed = reversed + original.charAt(i); } return original.equals(reversed); } }
Output:
Enter a String: mam mam Is a Palindrome
Find Palindrome Using toCharArray
package com.softwaretestingo.interviewprograms; import java.util.Scanner; public class PalindromeNumberEx7 { public static void main(String[] args) { String originalString; Scanner sc=new Scanner(System.in); System.out.println("Enter a String or Number: "); originalString=sc.nextLine(); if (isPalindrome(originalString.toLowerCase())) System.out.println(originalString+ " Is a Palindrome"); else System.out.println(originalString+ " Is Not a Palindrome"); } private static boolean isPalindrome(String original) { char[] data = original.toCharArray(); int i = 0; int j = data.length - 1; while (j > i) { if (data[i] != data[j]) { return false; } ++i; --j; } return true; } }
Output:
Enter a String or Number: 151 151 Is a Palindrome
Find Palindrome Using Stack
package com.softwaretestingo.interviewprograms; import java.util.Scanner; import java.util.Stack; public class PalindromeNumberEx8 { public static void main(String[] args) { String originalString; Scanner sc=new Scanner(System.in); System.out.println("Enter a String or Number: "); originalString=sc.nextLine(); if (isPalindrome(originalString.toLowerCase())) System.out.println(originalString+ " Is a Palindrome"); else System.out.println(originalString+ " Is Not a Palindrome"); } private static boolean isPalindrome(String original) { char[] data = original.toCharArray(); Stack stack = new Stack<>(); for (char c: data) { stack.push(c); } char[] data2 = new char[data.length]; int len = stack.size(); for (int i = 0; i < len; i++) { data2[i] = (char) stack.pop(); } String reversed = new String(data2); return original.equals(reversed); } }
Output:
Enter a String or Number: madam madam Is a Palindrome
Conclusion:
Here we come to the end of the Post. In this post, we have discussed What palindrome numbers and palindrome strings are. And also various possible ways to find out the palindrome of an integer. If still struggling, then you can ask in the comment section.