For a String if the reverse of a String is the same as the original String, then that String is called a Palindrome String

Example : Malayalam

For a Number if the reverse of a Number is equal to the same Number, then that number is called a Palindrome Number

Example : 4554, 1221

Following programs illustrate whether a string or a number is a Palindrome or not

1) Java Program to Check Palindrome String

import java.util.Scanner;

public class PalindromeString {

	private static Scanner s_char;

	public static void main(String[] args) {    	 
		String original_str, reverse_str = "";
		s_char = new Scanner(System.in);   
	    System.out.println("Enter a string/number to check a palindrome");  
	    original_str = s_char.nextLine();   
	    int str_length = original_str.length();   


		for ( int i = str_length - 1; i >= 0; i-- )  
	         reverse_str = reverse_str + original_str.charAt(i);  

		
	    if (original_str.equals(reverse_str))  
	         System.out.println("Entered string/number is a palindrome.");  

	    
	    else  
	         System.out.println("Entered string/number isn't a palindrome.");  

	    
	  }
}

Output 1:

Enter a string/number to check a palindrome
malayalam
Entered string/number is a palindrome.

Output 1:

Enter a string/number to check a palindrome
Sachin
Entered string/number isn't a palindrome.

2) Java Program to Check Palindrome Number

import java.util.Scanner;

public class PalindromeNumber {
	private static Scanner s_num;

	  public static void main(String[] args) {
	    
	    int reversed_num = 0, remainder;

	    s_num = new Scanner(System.in);   
		
		System.out.println("Enter a number to check a palindrome");  
		int  original_number = s_num.nextInt();   
		int number = original_number;   
	    while (number != 0) {
	      remainder = number % 10;
	      reversed_num = reversed_num * 10 + remainder;
	      number /= 10;
	    }
	    
	    if (original_number == reversed_num) {
	      System.out.println(original_number + " is Palindrome.");
	    }
	    else {
	      System.out.println(original_number + " is not Palindrome.");
	    }
	  }
}


Output 1 :

Enter a number to check a palindrome
4554
4554 is Palindrome.

Output 2 :

Enter a number to check a palindrome
4563
4563 is not Palindrome.
Scroll to Top