Palindrome Number Program in Java

Previously we have developed many java programs based on Armstrong numbers. Now in this post, we will develop a palindrome number program in java. It will check the given number is palindrome number or not, and later we will develop an another Java program which will find all palindrome number in a between two numbers.

Palindrome number:- If the Reverse of a number is equal to the same number then the number is called palindrome number.

Example of palindrome number:-
5225 = 5225 So, 5225 is a palindrome number.
123 = 321 So, 123 is not a palindrome number.

This program completely depends on the program to find the reverse of a number. After finding the reverse of a number compare the result and the actual number if both are the same then the given number is a palindrome number else the number is not a palindrome number.

Prerequisite:- Java program to find reverse of a number

The process to check number is a palindrome number or not,
1) Take a number
2) Find the reverse of the number
3) If reverse and the actual number is the same then it a palindrome number
4) Else it is not a palindrome number

Palindrome Number Program in Java

import java.util.Scanner;

public class PalindromeNumber {

   // returns reverse of a number
   public static int findReverse(int num) {

      // declare a temporary variable and
      // initialize it with 0
      int reverse = 0;

      // loop to repeat the process
      while(num != 0) {

         // find last digit of number
         // multiply temporary variable by 10
         // add last digit to temporary variable
         reverse = (reverse*10 ) + (num%10);

         // remove last digit
         num = num / 10;
      }
      return reverse;
   }

   // returns true if number is Palindrome  
   public static boolean isPalindrome(int n) {
      return (findReverse(n) == n) ? true : false;
   }

   public static void main(String[] args) {

      // declare variable
      int number = 0;

      // create Scanner class object 
      Scanner scan = new Scanner(System.in);

      // take input
      System.out.print("Enter an integer number: ");
      number = scan.nextInt();

      // check number is palindrome or not
      if(isPalindrome(number))
         System.out.println(number+
                " is a palindrome number");
      else
         System.out.println(number+
                " is not a palindrome number");

      // close Scanner class object
      scan.close();
   }
}

The output for different test-cases:-

Enter an integer number: 12345
12345 is not a palindrome number

Enter an integer number: 1234321
1234321 is a palindrome number

In this program, the isPalindrome(-) method also can be written using if-else,

public static boolean isPalindrome(int n) {
     if(findReverse(n) == n)
         return true;
     else 
         return false;
 }

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Java program to find all palindrome number in a given range

We can also find all the palindrome numbers between the two given numbers. For this purpose, we need to use the loop.

import java.util.Scanner;

public class PalindromeNumberInRange {

  // returns reverse of a number
  public static int findReverse(int num) {

     // declare a temporary variable and
     // initialize it with 0
     int reverse = 0;

     // loop to repeat the process
     while(num != 0) {

        // find last digit of number
        // multiply temporary variable by 10
        // add last digit to temporary variable
        reverse = (reverse*10 ) + (num%10);

        // remove last digit
        num = num / 10;
     }
     return reverse;
  }

  // returns true if number is Palindrome  
  public static boolean isPalindrome(int n) {
     return (findReverse(n)==n) ? true : false;
  }

  public static void main(String[] args) {

     // declare variables
     int minRange , maxRange;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter min Range value::");
     minRange = scan.nextInt();
     System.out.print("Enter max Range value::");
     maxRange = scan.nextInt();

     // find in the given range
     System.out.println("Palindrome numbers "+
        "from "+minRange+" to "+maxRange+":: ");

     for(int i = minRange; i<= maxRange; i++) {
        if(isPalindrome(i)) 
           System.out.print( i + " ");
     }

     // close Scanner class object
     scan.close();
  }
}

The output for different test-cases:-

Enter min Range value:: 1
Enter max Range value:: 100
Palindrome numbers from 1 to 100::
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99

Enter min Range value:: 100
Enter max Range value:: 200
Palindrome numbers from 100 to 200::
101 111 121 131 141 151 161 171 181 191

Enter min Range value:: 1000
Enter max Range value:: 2000
Palindrome numbers from 1000 to 2000::
1001 1111 1221 1331 1441 1551 1661 1771 1881 1991

Another Way to Check the Palindrome Number

The idea is to compare the first digit with the last digit. Similarly, the second digit of the number with the (last-1) digit of the number, and so on… If all returns true then the given number is a palindrome number else it is not a palindrome number.

In this method first, we need to find the total number of digits in the given number. We need to compare only up to number_of_digits/2

If the number of digits in the given number is odd then no need to compare the middle number.

import java.util.Scanner;

public class PalindromeNumberAnotherWay {

  // returns true if number is Palindrome  
  public static int findNumberOfDigits(int n) {

     int digits = 0;
     while(n != 0) {
        digits++;
        n /= 10;
     }

     return digits;
  }

  public static boolean isPalindrome(int num) {

     // declare the variables
     int leading_digit = 0;  // first digit
     int trailing_digit = 0; // last digit
     int n = num; // variable to store number

     // count number of digits in the number
     int digit_count = findNumberOfDigits(num);

     // Find appropriate divisor to extract 
     // the leading digit
     int divisor =(int)Math.pow(10,digit_count-1);

     // loop
     for(int i=1; i<=digit_count/2; i++) {

        leading_digit=n/divisor; //first digit
        trailing_digit = n%10; // last digit

        // compare first and last digit 
        if(leading_digit != trailing_digit)
            return false;

        n = n % divisor; // remove first digit
        n = n / 10; // remove last digit

        // last digit &amp; first digit are removed 
        // so also divide the divisor by 10*10
        divisor /= 100; 
     }
     return true;
  }

  public static void main(String[] args) {

     // declare variable
     int number = 0;

     // create Scanner class object 
     Scanner scan = new Scanner(System.in);

     // take input
     System.out.print("Enter an integer number:");
     number = scan.nextInt();

     // check number is palindrome or not
     if(isPalindrome(number))
        System.out.println(number+
               " is a palindrome number");
     else
        System.out.println(number+
              " is not a palindrome number");

     // close Scanner class object
     scan.close();

   }
}

We can also use String to check the given number is a palindrome number or not, See:- Palindrome String in Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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