Java program to check if a number is palindrome or not

Let’s learn java program to check if a number is palindrome or not.

Java program to check if a number is palindrome or not

Check whether number is palindrome or not is one of the common java interview question.

Palindrome number is a number where number remain the same when its numbers are reversed.

For example: 141, 101, 181, 12321 etc..

Here first user enters a number. This number is stored in temporary variable. Then number is reversed. Next compare temporary number with reversed number.

Also read – armstrong program in java

If both temporary number and reversed number are same then it is a palindrome number else it’s not a palindrome number.

Now let’s see java program on palindrome number in java,

import java.util.Scanner;

public class PalindromeNumberInJava
{
   public static void main(String[] args) 
   { 
      System.out.println("Please enter a number to reverse : ");
      Scanner sc = new Scanner(System.in);
      int reverse, total = 0, temp;
      int number = sc.nextInt();
      temp = number; 
      while(number > 0)
      { 
         reverse = number % 10; 
         total = (total * 10) + reverse; 
         number = number / 10;
      } 
      if(temp == total)
      {
         System.out.println(total + " is a Palindrome Number");
      }
      else
      {
         System.out.println(total + " not a palindrome number");
      }
      sc.close();
   }
}



Output:

java program to check if a number is palindrome or not

String palindrome in java using array

It is possible to execute palindrome number program in java using string as well. Let’s see string palindrome in java using array.

Also read – prime number program in java

To check a string for palindrome using arrays in java first we have to convert user entered string into character array using toCharArray() method.

Then make a copy of this character array and reverse the array. Lastly compare given array with reversed array. If both array matches then string is palindrome string else it is not.

Now let’s see java program,

import java.util.Arrays;
import java.util.Scanner;

public class PalindromeStringDemo 
{
   public static void main(String[] args) 
   {
      System.out.println("Please enter a string - ");
      Scanner sc = new Scanner(System.in);
      String strInput = sc.nextLine();
      char[] chReverse = strInput.toCharArray();
      int size = chReverse.length;
      char[] chOriginal = Arrays.copyOf(chReverse, chReverse.length);
      
      for(int a = 0; a < size / 2; a++) 
      {
         char temp = chReverse[a];
         chReverse[a] = chReverse[size - a - 1];
         chReverse[size - a - 1] = temp;
      }
      System.out.println("Original Array: " + Arrays.toString(chOriginal));
      System.out.println("Reverse Array: " + Arrays.toString(chReverse));
      
      if(Arrays.equals(chReverse, chOriginal)) 
      {
         System.out.println("String is palindrome");
      }
      else 
      {
         System.out.println("String is not palindrome");
      }
      sc.close();
   }
}



Output:

Please enter a string – madam
Original Array: [m, a, d, a, m]
Reverse Array: [m, a, d, a, m]
String is palindrome

Without using reverse method

Let’s learn string palindrome in java without using reverse method. In java program to check if any string is palindrome without using reverse method first convert user entered string to char array.

Also read – factorial program in java

In the next step read char from char array from last index. Lastly concatenate. Let’s see java program,

import java.util.Scanner;

public class StringPalindromeDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter string to check palindrome: ");
      String strInput = sc.nextLine();
      // converting string to char array
      char[] chString = strInput.toCharArray();       
      // storing reverse string
      String strReverse = "";         
      // reading char by char
      for(int a = chString.length - 1; a >= 0; a--) 
      {
         strReverse = strReverse + chString[a];
      } 
      // printing given string and reversed string
      System.out.println("Given string: " + strInput);
      System.out.println("Reverse String: " + strReverse); 
      // check if given string is palindrome
      if(strInput.equals(strReverse))
      {
         System.out.println("string is palindrome.");
      }
      else
      {
         System.out.println("string is not palindrome.");
      }
   }
}



Output:

Please enter string to check palindrome: madam
Given string: madam
Reverse String: madam
string is palindrome.

Please enter string to check palindrome: abcd
Given string: abcd
Reverse String: dcba
string is not palindrome.