Palindrome Program in Java
Check Whether or Not the Number is a Palindrome in Java
Given an integer input as the number, the objective is to check whether or not the given number is a palindrome. To do so, we’ll first reverse the string input using loops and recursion and check if it matches the original number.
Example Input : 121 Output : Palindrome
Check Whether or Not the Number is a Palindrome in Java Language
Given an input integer as a number, the objective is to check whether or not the given number integer is a Palindrome or not in Java Language. To do so we’ll reverse the the number using the modulo and divide operators and check if the reversed number matches the original number. Here are few methods to Check Whether or Not the Number is a Palindrome in Java Language,
- Method 1: Using Iteration
- Method 2: Using Recursion
Let’s discuss the above mentioned methods in detail in the upcoming sections. Don’t forget to check the blue box mentioned below for better understanding of the problem.
Method 1: Using Iteration
Working
In this method we’ll use loops to break down the number into individual digits and the reassemble them in reverse order. We’ll use the modulo operator to extract the digits and divide operator to shorten the number. Finally we’ll compare if the reversed number matches the original number.
For a given integer number variable, we perform the following operations,
- Run a while loop until the condition temp variable is not 0.
- Extract the digits using modulo operator.
- Using the formula reverse = reverse * 10 + last digit, we’ll keep updating the reverse variable.
- We’ll shorten the number using divide operator.
- Check if the reversed number matches the original number.
Let’s implement the above mentioned logic in Java Language.
Table of Contents
Java Code
public class Main { public static void main (String[]args) { //variables initialization int num = 12021, reverse = 0, rem, temp; temp = num; //loop to find reverse number while (temp != 0) { rem = temp % 10; reverse = reverse * 10 + rem; temp /= 10; }; // palindrome if num and reverse are equal if (num == reverse) System.out.println (num + " is Palindrome"); else System.out.println (num + " is not Palindrome"); } }
Output
12021 is Palindrome
Method 2: Using Recursion
Working
In this method we’ll use recursion to break down the number into individual digits and then reassemble them in reverse order. In the end we’ll check if the reversed number matches the original number.
For a given integer input as number, we perform the following,
- Define a recursive function which takes the number as an argument.
- Set the base case as number ==0.
- Set the recursive step call as getReverse(num/10, rev).
- Check if the returned value matches the original number.
Let’s implement the above mentioned logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { //variables initialization int num = 12021, reverse = 0, rem, temp; // palindrome if num and reverse are equal if (getReverse(num, reverse) == num) System.out.println (num + " is Palindrome"); else System.out.println (num + " is not Palindrome"); } static int getReverse(int num, int rev){ if(num == 0) return rev; int rem = num % 10; rev = rev * 10 + rem; return getReverse(num / 10, rev); } }
Output
21021 is Palindrome
Advanced Methods
The above programs are algorithmic methods that will work with numbers.
The below programs may be a little advanced but will work with both numeric and string inputs
- Method 3 : Using StringBuilder
- Method 4: Using for Loop and charAt
- Method 5: Using toCharArray
- Method 6: Using Stack
Method 3: Using StringBuilder
Explanation of the code mentioned in the comments below –
class Main { public static void main(String[] args) { // convert these strings to either lowercase or uppercase to create consistency System.out.println(isPalindrome("radar".toLowerCase())); // Naman wouldn't have been palindrome if toLowerCase method wasn't used System.out.println(isPalindrome("Naman".toLowerCase())); System.out.println(isPalindrome("12321".toLowerCase())); System.out.println(isPalindrome("12345".toLowerCase())); } private static boolean isPalindrome(String string) { String reversed = new StringBuilder(string).reverse().toString(); return string.equals(reversed); } }
Output
true
true
true
false
Method 4: Using for loop and charAt
Explanation of the code mentioned in the comments below –
class Main { public static void main(String[] args) { // convert these strings to either lowercase or uppercase to create consistency System.out.println(isPalindrome("radar".toLowerCase())); // Naman wouldn't have been palindrome if toLowerCase method wasn't used System.out.println(isPalindrome("Naman".toLowerCase())); System.out.println(isPalindrome("12321".toLowerCase())); System.out.println(isPalindrome("12345".toLowerCase())); } 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
true
true
true
false
Method 5: Using with toCharArray
Explanation of the code mentioned in the comments below –
class Main { public static void main(String[] args) { // convert these strings to either lowercase or uppercase to create consistency System.out.println(isPalindrome("radar".toLowerCase())); // Naman wouldn't have been palindrome if toLowerCase method wasn't used System.out.println(isPalindrome("Naman".toLowerCase())); System.out.println(isPalindrome("12321".toLowerCase())); System.out.println(isPalindrome("12345".toLowerCase())); } 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
true
true
true
false
Method 6: Using Stack
Explanation of the code mentioned in the comments below –
import java.util.Stack; class Main { public static void main(String[] args) { // convert these strings to either lowercase or uppercase to create consistency System.out.println(isPalindrome("radar".toLowerCase())); // Naman wouldn't have been palindrome if toLowerCase method wasn't used System.out.println(isPalindrome("Naman".toLowerCase())); System.out.println(isPalindrome("12321".toLowerCase())); System.out.println(isPalindrome("12345".toLowerCase())); } 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] = stack.pop(); } var reversed = new String(data2); return original.equals(reversed); } }
Output
true
true
true
false
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Working with Numbers
- Highest Common Factor(HCF): C | C++ | Java | Python
- Lowest Common Multiple (LCM) : C | C++ | Java | Python
- Greatest Common Divisor : C | C++ | Java | Python
- Binary to Decimal to conversion : C | C++ | Java | Python
- Octal to Decimal conversion : C | C++ | Java | Python
- Hexadecimal to Decimal conversion: C | C++ | Java | Python
- Decimal to Binary conversion: C | C++ | Java | Python
- Decimal to Octal Conversion: C | C++ | Java | Python
- Decimal to Hexadecimal Conversion: C | C++ | Java | Python
- Binary to Octal conversion : C | C++ | Java | Python
- Octal to Binary conversion : C | C++ | Java | Python
- Quadrants in which a given coordinate lies : C | C++ | Java | Python
- Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
- Maximum number of handshakes: C | C++ | Java | Python
- Addition of two fractions: C | C++ | Java | Python
- Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
- Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
- Count possible decoding of a given digit sequence : C | C++ | Java | Python
- Check whether a character is a vowel or consonant : C | C++ | Java | Python
- Check whether a character is a alphabet or not : C | C++ | Java | Python
- Calculate the area of a circle : C | C++ | Java | Python
- Find the ASCII value of a character : C | C++ | Java | Python
- Find the prime numbers between 1 to 100 : C | C++ | Java | Python
- Calculate the number of digits in an integer : C | C++ | Java | Python
- Convert digit/number to words : C | C++ | Java | Python
- Counting number of days in a given month of a year: C | C++ | Java | Python
- Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
- Finding number of integers which has exactly x divisors: C | C++ | Java | Python
- Finding Roots of a quadratic equation : C | C++ | Java | Python
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
Class satendr{
Public static void main (String[]args]{
String s=” raja” ;
String rev=” “;
for (int i= s.length()-1; I>=0; i- -){
rev= rev+ s.charAt(I);
}
if(s equals (rev)){
System.out.println(” this is palindrome char: ” + rev);
}
else{
System.out.println(” not palindrome char: “+ s );
}}}
this there’s something wrong in for loop
it should be like this
for(int i=length-1;i>=0;i–)
We have fixed the code thanks for the suggestion
st1 = st1 +ch;