Java Palindrome Program- Check String or Number is Palindrome
In this tutorial we will learn Java Palindrome Program used to check whether the given input String or Number is Palindrome or not.
Java Palindrome Program- Check String is Palindrome
A string is called as palindrome string when you reverse the given input string and it results same as the original input string.
For example, madam, level etc., are all palindrome string i.e., if you reverse those string the result will be same as the original one.
Below is the program to check if the given string is Palindrome or not in Java programming.
package com.sneppets.programming; import java.util.*; public class PalindromeDemo { public static void main (String[] args) { boolean isPalindrome = true; System.out.println("Enter input string/number :"); Scanner sc = new Scanner(System.in); String S = sc.next(); for(int i=0; i<S.length()/2; i++) { if(S.charAt(i)!=S.charAt(S.length()-i-1)) { isPalindrome = false; } } if(isPalindrome) { System.out.println("Input string is Palindrome"); }else { System.out.println("Input string is NOT Palnindrome"); } } }
Output:
Enter input string : madam Input string is Palindrome
For instance, let’s check another string “hello” whether it is palindrome.
Enter input string : hello Input string is NOT Palnindrome
Hence, string “hello” is not a palindrome string.
Check Number is Palindrome
Similarly, when you reverse a number and the result is same as the original number, then the given number is Palindrome number. For example, 101, 4554 etc., are palindrome numbers.
The same program you see above can be used to check whether the given number is palindrome or not.
For instance, let’s check whether 4554 is palindrome or not.
Enter input string : 4554 Input string is Palindrome
And let’s check another number 3456
Enter input string : 3456 Input string is NOT Palnindrome
That’s it. You had learnt how to write Java palindrome program to check whether given input string or number is palindrome or not.
Hope it helped 🙂
You’ll also like:
- Java 8 : Find union and intersection of two Lists (ArrayLists)
- How to manipulate Java 8 Stream data sources or objects?
- How to copy contents of a List to another List in Java
- Replace element in ArrayList at specific index
- Convert Integer List to int array
- ArrayList removeAll() method not removing elements
- Convert floating point number to fixed point in Python
- What is %matplotlib inline and how to use ?
- Java String substring() example program
- Add python3 kernel to jupyter IPython notebook ?
- Reset jupyter notebook theme to default theme
- How to change the default theme in Jupyter Notebook ?
- To run Jupyter Notebook on Windows from command line
- Run a Jupyter Notebook .ipynb file from terminal or cmd prompt
- Amazon Linux AMI : apt-get command not found
- Check if a file or folder exists without getting exceptions ?
- Python program to find the greatest of three numbers
- Putty Fatal Error No supported authentication methods available
- Find which users belongs to a specific group in linux
- Check if Python Object is a Number ?
- Remove non-numeric characters from string in Python
- Convert negative to positive number in Python
- Extract numbers from a string in python
- Add or install local jar files to the Maven Project ?