Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Java Program to Check Whether a Number is a Palindrome or Not

In this article, we will discuss what palindrome numbers are and how to write a program to check whether a given number is a palindrome or not in Java. We will discuss various approaches to check whether a given number is a palindrome or not.

Given a number N we need to find whether the number is a palindrome or not. A palindrome number is a number that remains as it is even if we reverse the number. Examples are given to understand problem statement batter.

Input 1:- 2112
Output 1:- The Number is a palindrome

Input 2:- 1234
Output 2:- The Number is not a palindrome

What are Palindrome Numbers

A palindrome number is a number that is equal to its reverse number. We considered some examples below to understand what is a palindrome number.

Given Number      =  2112
Reverse Number  =  2112
Thus, the given number (2112) and the reverse number (2112) are the same so the number 2112 is a palindrome

Given Number      =  1234
Reverse Number  =  4321
Here, the given number (1234) and the reverse number (4321) are not the same so the number 1234 is not a palindrome

Algorithm to Check whether a Number is Palindrome or not

Below are the major steps for the algorithm.

  • Create a new variable to store the reverse of a given number
  • Implement logic to get a reverse number from a given number
  • Compare a given number and a reverse number
  • If both numbers are the same then a given number is palindrome else not

Let’s understand the algorithm with help of an example and a dry run.

Given Number N= 1441

Step 1:- Initialize Reverse Number reverse_number=0
Step 2:- Reverse a given number and store it in reverse_number. The logic to reverse a number is given below.

  • Run a while loop until N becomes 0.
  • Get last digit of N (N%10) and add it into reverse_number (reverse_number*10+last digit)
  • Remove the last digit of N (N/10)

    N=1441 reverse_number=0

    N (1441) > 0:
    last _digit = N%10 = 1441%10 = 1

    reverse_number= reverse_number10+last_digit = 010+1 = 1

    N= N/10 = 1441/10 = 144

    N (144) > 0:
    last _digit = N%10 = 144%10 = 4

    reverse_number= reverse_number10+last_digit = 110+4 = 14

    N= N/10 = 144/10 = 14

    N (14) > 0:
    last _digit = N%10 = 14%10 = 4

    reverse_number= reverse_number10+last_digit = 1410+4 = 144

    N= N/10 = 14/10 = 1

    N (1) > 0:
    last _digit = N%10 = 1%10 = 1

    reverse_number= reverse_number10+last_digit = 14410+1 = 1441

    N= N/10 = 1/10 = 0

    N (0) > 0 False:
    break while loop.

Now, we got a reverse of a given number.

Step 3:- Now, compare the given number and the reverse number. Here the given number N=1441 and the reverse of the given number reverse_number=1441 both are the same thus our answer is true.

Palindrome Program in Java:-

Let’s discuss an approach to solving the palindrome program in java. First, we will discuss palindrome in java using reverse numbers.

Method to Check Palindrome in Java using Reverse Number

//palindrome program in java
class Prepbytes{
    // method to reverse number
    public static int reverse_number(int n){
        int reverse_n=0;
        while(n>0){
            reverse_n=reverse_n*10+n%10;
            n=n/10;
        }
        return reverse_n;
    }
    
    // method to check palindrome in java
    public static boolean is_palindrome(int n){
        // first we will find the reverse number
        int reverse_n=reverse_number(n);
        // if the reverse number and the given number are the same then the number is a palindrome
        if(reverse_n==n){
            return true;
        }
        else{
            return false;
        }
    }
    
    public static void main(String[] args) {
        int n=8118;
        if(is_palindrome(n)==true){
            System.out.println("Number N: "+n+" is palindrome a number");
        }
        else{
            System.out.println("Number N: "+n+" is not a palindrome number");
        }
        int n1=1234;
        if(is_palindrome(n1)==true){
            System.out.println("Number N: "+n1+" is palindrome a number");
        }
        else{
            System.out.println("Number N: "+n1+" is not a palindrome number");
        }
    }
}
Output:-
Number N: 8118 is a palindrome number
Number N: 1234 is not a palindrome number

In the above program to check palindrome in java first, we have created a method to find a reverse number of a given number in which we run a while loop until the given number is greater than 0 after that we will reverse number * 10, and n%10 (last digit) into reverse number. After that, we will divide n by 10 and run a loop until n is greater than zero. We have also created a method to check given number is a palindrome in java or not in that method we first call the reverse_number method to find a reverse number of a given number.

Time Complexity:- O(logn) we are using while loop to reverse the given number which will be executed log n times.

Space Complexity:- O(1) we are not using any extra data structure to store any data.

Method to Check Palindrome in Java using String.

// program to check palindrome in java
class Prepbytes{
    // method to check palindrome in java
    public static boolean is_palindrome(int n){
        // convert the given number into a string
        String str_n=Integer.toString(n);
        int length=str_n.length();
        // run loop till half of the length to check ith and length-i-1th char are the same
        for(int i=0;i<length/2;i++){
            // if the char is not the same then return false
            if(str_n.charAt(i)!=str_n.charAt(length-i-1)){
                return false;
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        int n=8118;
        if(is_palindrome(n)==true){
            System.out.println("Number N: "+n+" is palindrome a number");
        }
        else{
            System.out.println("Number N: "+n+" is not a palindrome number");
        }
        int n1=1234;
        if(is_palindrome(n1)==true){
            System.out.println("Number N: "+n1+" is palindrome a number");
        }
        else{
            System.out.println("Number N: "+n1+" is not a palindrome number");
        }
    }
}
Output:-
Number N: 8118 is palindrome a number
Number N: 1234 is not a palindrome number

In the above palindrome program in java, first, we created a method that converts a given number into a string and then we run for loop to see if the first and last, second and second last and so on characters are matching if it is not matching then we will return false else we will return true.

Time Complexity:- O(logn) we are running a loop from 0 to length of string/2 which is equal to log(n)/2 so we can say effective time complexity as log(n).

Space Complexity:- O(1) we are not using any extra data structure to store any data.

Method to Finding Palindrome in java Using Recursion.

// palindrome program in java
class Prepbytes{
    // recursive function to find the reverse of a given number
    public static int reverse_number(int n,int rev_num){
        if(n==0){
            return rev_num;
        }
        rev_num=rev_num*10+n%10;
        return reverse_number(n/10,rev_num);
    }
    
    // a method to check if a given number is palindrome or not
    public static boolean is_palindrome(int n){
        int rev_num=reverse_number(n,0);
        if(n==rev_num){
            return true;
        }
        else{
            return false;
        }
    }
    
    public static void main(String[] args) {
        int n=8118;
        if(is_palindrome(n)==true){
            System.out.println("Number N: "+n+" is palindrome a number");
        }
        else{
            System.out.println("Number N: "+n+" is not a palindrome number");
        }
        int n1=1234;
        if(is_palindrome(n1)==true){
            System.out.println("Number N: "+n1+" is palindrome a number");
        }
        else{
            System.out.println("Number N: "+n1+" is not a palindrome number");
        }
    }
}
Output:-
Number N: 8118 is palindrome a number
Number N: 1234 is not a palindrome number

In the above program to check palindrome in java, first, we created a recursive method to find the reverse number of the given number in which our base case if when n becomes zero at that time we return rev_num if it is not the base case then we add the last digit of the given number n at the end of the rev_num and call recursive method until we reach the base case. We also created a method to check whether a given number is a palindrome number or not in which we first find the reverse number of a given number and then we check if the reverse number and given number are the same if it is true then we return true else we return false.

Time Complexity:- O(logn) we making log(n) recursive calls to find the reverse number.

Space Complexity:- O(1) we are not using any extra data structure to store any data.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List

Leave a Reply

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