Palindrome Program in Java

In this post we will learn how to check whether a number or string is palindrome and here we have discussed all possible ways step by step. But before entering the coding part we should have clear idea that what is palindrome number and what is palindrome string.

What is Palindrome number?

A number is said to be a palindrome if the number is same after reverse.

For example – 1112199945654.

What is Palindrome String?

A String is said to be a palindrome if the String is same after reverse.

For example – MOMMADAMLEVELMALAYALAM.

Palindrome Number in java

Palindrome number program in java.

String palindrome program in java.

Check palindrome number in java using for loop.

Algorithm :-

  • Take the number that needs to be checked for Palindrome.
  • Store the number into temporary variable.
  • Reverse the number (using for loop).
  • Compare reverse number with temporary number.
  • If both numbers are the same, print “Given Number is palindrome”.
  • Else, print “Given number is not palindrome”.
class Palindrome
{  
 public static void main(String args[])
 {  
  int remainder;
  int sum=0;
  int number = 12521;//This is the number that needs to be checked  
  int temp = number; // holding input number in temp variable.
  for( ; number!=0;number/=10 ) //number/=10, means number=number/10;
  {    
   remainder = number%10;  //getting remainder.
   sum = (sum*10)+remainder; // getting reverse.
  }    
  if(temp == sum) //Comparing original number(temp) with reversed number(sum).
     System.out.println(temp+" is Palindrome Number.");    
  else    
     System.out.println(temp+" is Not Palindrome Number.");    
  }  
}

Output :-

Palindrome Number check using for loop in java

Check whether input number is Palindrome or not using while loop in java.

Algorithm :

  • Take the number that needs to be checked for Palindrome.
  • Store the number into temporary variable.
  • Reverse the number(using while loop).
  • Compare reverse number with temporary number.
  • If both numbers are the same, print “Input number is a palindrome”.
  • Else print “Input number is not a palindrome”.
import java.util.*;   
 class Palindrome{  
 public static void main(String args[]){  
  int r,sum=0;    
  Scanner scan = new Scanner(System.in);
  System.out.println("Please Enter the number");
  int number = scan.nextInt(); // taking input from user.
  int temp = number; // holding input number in temp variable.
  while(number>0){    
   r = number%10;  //getting remainder.
   sum = (sum*10)+r; // getting reverse.
   number = number/10;    
  }    
  if(temp == sum) //Comparing original number(temp) with reversed number(sum).
   System.out.println(temp+" is Palindrome Number.");    
  else    
   System.out.println(temp+" is Not Palindrome Number.");    
}  
}

Output :-

Check Palindrome number using while loop

Palindrome String in java.

In this example we will check given string is palindrome or not using array.

Example -1

Algorithm :-

  • Convert the given string into a character array.
  • Hold this array in temporary variable.
  • Reverse the array.
  • Compare the original array and the reversed array.
  • If both strings are the same, print “Given string is palindrome”.
  • Else, print “Given number is not palindrome”.
import java.util.Arrays;
import java.util.Scanner;
class Palindrome {
   public static void main(String args[]) {
     
      Scanner scan = new Scanner(System.in);
	  System.out.println("Please Enter a string:");
      String str = scan.nextLine();	  
      char[] chArray = str.toCharArray();//toCharArray() method is used to convert string to character array.
      int size = chArray.length;
      char [] originalArray = Arrays.copyOf(chArray,size);//Arrays.copyOf() method is used to copy the array.

      for (int i = 0; i < size / 2; i++)
      {
         char temp = chArray[i];
         chArray[i] = chArray[size-i-1];
	     chArray[size-i-1] = temp;
	  }
      System.out.println("Original Array = "+Arrays.toString(originalArray));
      System.out.println("Reverse Array = "+Arrays.toString(chArray));

      if(Arrays.equals(chArray, originalArray))//Arrays.equals() method has used to check whether two arrays are equal. 
      {
         System.out.println("Input string is a palindrome");
      } else {
         System.out.println("Input string is not a palindrome");
      }
   }
}

Output :

Palindrome string using array

Check given String is palindrome or not in java using array (Another way).

Example – 2

Algorithm :

  • Convert the given string into a character array.
  • Hold this array in temporary variable.
  • Reverse the array.
  • Compare the original array and the reversed array.
  • If both strings are the same, print “Given string is palindrome”.
  • Else, print “Given number is not palindrome”.
import java.util.Arrays;
import java.util.Scanner;
class Palindrome {
   public static void main(String args[]) {
     
      Scanner scan = new Scanner(System.in);
	  System.out.println("Please Enter a string:");
      String str = scan.nextLine();	  
      char[] chArray = str.toCharArray();//toCharArray() method is used to convert string to character array.
      int size = chArray.length;
      char [] originalArray = Arrays.copyOf(chArray,size);//Arrays.copyOf() method is used to copy the array.

      for (int i = 0; i < size / 2; i++)
      {
         char temp = chArray[i];
         chArray[i] = chArray[size-i-1];
	     chArray[size-i-1] = temp;
	  }
      System.out.println("Original Array = "+Arrays.toString(originalArray));
      System.out.println("Reverse Array = "+Arrays.toString(chArray));

      if(Arrays.equals(chArray, originalArray))//Arrays.equals() method has used to check whether two arrays are equal. 
      {
         System.out.println("Input string is a palindrome");
      } else {
         System.out.println("Input string is not a palindrome");
      }
   }
}

Output :-

Palindrome string using simple array

Check if a string is palindrome or not in java using reverse method.

Algorithm :-

  • Get the String to check for palindrome.
  • Reverse the string (using string reverse function).
  • Compare the Original string with reversed string.
  • If both strings are the same, print “This is palindrome string”.
  • Else, print “This is not palindrome string”.
import java.util.*;
class Palindrome{
  public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a string to check if it is a palindrome:");
    String input = scan.nextLine();
    String reverse = new StringBuffer(input).reverse().toString();//The reverse() method reverses an array in place
    if (input.equals(reverse))
    {
      System.out.println("Yes, it is a palindrome");
    }else{
      System.out.println("No, it is not a palindrome");
    }
}
}

Output :-

Check palindrome using reverse method

This is all about palindrome numbers and palindrome strings, We hope you have understood all possible ways how to check palindrome number and palindrome string in java, and if you have any doubt about this you can ask in the comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *