java palindrome program string number

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

java palindrome program string number

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:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments