Home >Java >javaTutorial >Palindrome in Java
String or a number is said to be a palindrome if it remains the same even after it is reversed. For example, ‘MADAM’ is a palindrome string since it is spelled ‘MADAM’ even if it is reversed. But in the case of ‘LUCKY’, this string is not palindrome as it is ‘YKCUL’ when it is reversed. Some of the palindrome numbers are 365563, 48984, 12321, 171, 88, 90009, 343, and some of the palindrome strings are MADAM, MALAYALAM, LOL, DAD, MOM, C++&++C, etc. Let us see the logic and implementation of palindrome in the following sections. In this topic, we are going to learn about Palindrome in Java.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
In order to check whether a number is a palindrome, the following algorithm can be used.
For example, let us take the number 353 as input.
353-> temp
Reversednumber: rev=353
If they are the same, then the number is said to be a palindrome number.
Else, the number is not a palindrome number.
i.e.
If(inputnum==rev) { then palindrome } Else not palindrome
There are several methods in order to check whether the given input number is a palindrome or not.
Let us look into each of them in detail:
Code:
//Java program to check whether a String is a Palindrome or not using For Loop import java.util.*; public class PalindromeNumberExample { //main method public static void main(String[] args) { int r=0 ; //reversed Integer int rem, num; //remainder and original number Scanner s = new Scanner(System.in); System.out.print("Enter number that has to be checked:"); num = s.nextInt(); //Store the number in a temporary variable int temp = num; //loop to find the reverse of a number for( ;num != 0; num /= 10 ) { rem = num % 10; // find the modulus of the number when divided by 10 r = r * 10 + rem; } //check whether the original and reversed numbers are equal if (temp == r) { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are equal " + temp + " is a palindrome number"); } else { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are not equal " + temp + " is not a palindrome number"); } } }
Output 1:
Here, as 353 is the same when reversed, it is considered as a palindrome.
Output 2:
Here, as 234 remains not the same when reversed, it is not considered a palindrome.
Code:
//Java program to check whether a number is a Palindrome or not using While Loop import java.util.*; public class PalindromeNumberExample { public static void main(String[] args) { int r=0, rem, num; Scanner s = new Scanner(System.in); System.out.print("Enter number that has to be checked:"); num = s.nextInt(); //Store the number in a temporary variable int temp = num; //loop to find the reverse of a number while( num != 0 ) { rem= num % 10; r= r * 10 + rem; num=num/10; } //check whether the original and reversed numbers are equal if (temp == r) { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are equal " + temp + " is a palindrome number"); } else { System.out.println(temp + " is input number"); System.out.println(r + " is the reversed number"); System.out.println("Since they are not equal " + temp + " is not a palindrome number"); } } }
Output 1:
Output 2:
Code:
//Java program to check whether a String is a Palindrome or not using Library method import java.util.*; public class PalindromeNumberExample { //Function to check whether the string is palindrome or not public static void PalindromeCheck(String str) { // reverse the input String String rev = new StringBuffer(str).reverse().toString(); // checks whether the string is palindrome or not if (str.equals(rev)) { System.out.println("input string is :" + str); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are equal, "+ str +" is a palindrome"); } else { System.out.println("input string is :" + str); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are not equal, "+ str +" is not a palindrome"); } } public static void main (String[] args) { PalindromeCheck("MALAYALAM"); } }
Output:
Here, the input string is passed in the program itself.
To check whether a string is a palindrome, the following program is also used.
Code:
//Java program to check whether a String is a Palindrome or not import java.util.*; public class PalindromeNumberExample { public static void main(String args[]) { String st, rev = ""; Scanner sc = new Scanner(System.in); System.out.println("Enter the string that has to be checked:"); st = sc.nextLine(); int len = st.length(); //length of the string for ( int i = len- 1; i >= 0; i-- ) rev = rev + st.charAt(i); if (st.equals(rev)) { System.out.println("input string is :" + st); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are equal, "+ st +" is a palindrome"); } else { System.out.println("input string is :" + st); System.out.println("Reversed string is :" + rev); System.out.println("Since the input and reversed string are not equal, "+ st +" is not a palindrome"); } } }
Output:
A number is said to be palindrome if it remains the same even when it is reversed. A palindrome can be checked in strings also. Some of the palindrome numbers and strings are MOM, MALAYALAM, DAD, LOL, 232, 1331, etc. In this document, several aspects of Palindrome are covered, such as algorithm, methods, implementation, etc.
The above is the detailed content of Palindrome in Java. For more information, please follow other related articles on the PHP Chinese website!