Home » Java Tutorial » Palindrome in JAVA with Examples

Palindrome in JAVA with Examples

The palindrome in java is a number that is the same as the last reversed. 

The palindrome numbers in java are, for instance, 676,26262, etc.

There exists palindrome string in java, such as LOL, PULLUP, MADAM, etc.

What logic is utilized behind the palindrome?

The logic or approach that examines if a given number value is not a palindrome or is a palindrome in java is represented as below:

  1. Collect the input number to be verified to be a Palindrome
  2. Copy and reverse the number into a provisional variable.
  3. Compare the initial number and the inverted number.
  4. If they are qual from backward and forward, then, the number is “palindrome”
  5. Otherwise, the given number is not a “palindrome”

The logic or approach for string palindrome in java is equal as mentioned above but the only difference is that, instead of a number, a string is replaced. It is mentioned as below:

  1. Collect the input string to be verified to be a Palindrome
  2. Copy and reverse the whole string into a provisional variable.
  3. Compare the initial string and the inverted string.
  4. If they are qual from backward and forward, then, the given string is “palindrome string in java”
  5. Otherwise, the given string is not a “palindrome string in java”

Ways to carry out Palindrome code in java

Here, we have discussed the following considerations that display different ways of testing Palindrome in Java:

  • Utilizing while Loop, Palindrome code
  • Utilizing for Loop, Palindrome code
  • Palindrome (for some given string) code, utilizing the Library Methodology

Let’s have a grasp on the individual illustrations mentioned above ways. They are as:

1. Utilizing while loop:

public class demo_jpald {
 
    public static void main(String args[]) {
 
        int j_numb = 676, rev_jint = 0, j_rem, orig_jint;
 
        orig_jint = j_numb;
 
        // Let’s store the inverted integer into some variable 
        while( j_numb != 0 )
        {
            j_rem = j_numb % 10;
           rev_jint = rev_jint* 10 + j_rem;
            j_numb /= 10;
        }
 
        // Let’s examine if the given number value is palindrome or not
        if (orig_jint == rev_jint)
            System.out.println(orig_jint + " This given no is a palindrome nummber");
        else
            System.out.println(orig_jint + " This given no is not a palindrome number");
    }
}

Output:

// 676 This given no is a palindrome number

Understanding: Enter the number you want to verify and save it in the parameter temporary(temp). Reverse the figure now and check whether or not the temp figure is the same as the number inverted. If both figures are, all the same, the palindrome figure will be shown, otherwise, the palindrome number will not be shown.

2. Utilizing for loop:

public class demo_jpald2 {
 
    public static void main(String args[]) {
 
        int j_numb = 236, rev_jint = 0, j_rem, orig_jint;
 
        orig_jint = j_numb;
 
        // Let’s store the inverted integer into some variable 
          for( ;j_numb != 0; j_numb /= 10 )
        {
            j_rem = j_numb % 10;
            rev_jint = rev_jint * 10 + j_rem;
        }
 
        // Let’s examine if the given number value is palindrome or not
        if (orig_jint == rev_jint)
            System.out.println(orig_jint + " This given no is a palindrome nummber");
        else
            System.out.println(orig_jint + " This given no is not a palindrome number");
    }
}

Output:

// 236 This given no is not a palindrome number

Understanding: The integer is not a palindrome in the above code. The concept stays the same, instead of just a while loop, only the “for” loop is being used. The j_numb / = 10 is implemented on every iterations and case j_numb! =0 will be reviewed.

3. Utilizing library methodology:

In this approach, the specified string is examined if it is a palindrome string in java or not. The below illustration displays the same.

public class demo_jpald_str {
    
    static boolean str_is_palid(String st1){
         // These are the pointers that are indicating to the start and finish           of the given string
         int j_i=0; int j_y = st2.length() -1;
 
         // Let’s compare the given string utilizing while loop
         while (j_i < j_y){
         // If the string is not equal or same
         if (st1.charAt(j_i) != st1.charAt(j_y))
         {return false};
         // Now increment the first indicator and at the same time, decrement or decrease another indicator
         j_i++;
         j_y--;
        }
        // If the given string is a palindrome then return true
        {return true};
}
 
    public static void main(String args[]) {
 
        String st1 = “Javacode”;
 
        // Let’s examine if the given value of string is palindrome or not
        if (demo_jpald_str(st1))
            System.out.println(“The specified string is a palindrome string”)
        else
            System.out.println(" This given string is not a palindrome string");
    }
}

Output:

// This given string is not a palindrome string

Understanding:

We have used the reverse function for a string in the above script to determine the reversed value of a given string and then equate it with the originally entered string. If both the strings are the same, the palindrome string will be displayed, otherwise, the palindrome string will not be displayed.

Do you know?
1. Java Switch Statement with Examples
2. Java Queue
3. String to Int Java
4. Super Keyword in Java
5. Fibonacci series in java
6. HashSet in Java with Examples
7. Math.random Java
8. Static Keyword in Java
9. Final Keyword in Java
10. Palindrome in Java

Pin It on Pinterest