Algorithm


1. Start
2. Accept a string as input
3. Initialize two pointers, one at the beginning and one at the end of the string
4. Repeat until the first pointer is less than the second pointer:
    a. Compare characters at the first and second pointers
    b. If they are not equal, the string is not a palindrome
    c. Move the first pointer to the right
    d. Move the second pointer to the left
5. If the loop completes without breaking, the string is a palindrome
6. Print or return the result
7. End

Code Examples

#1 Code Example- Java Program to Check Palindrome String

Code - Java Programming

class Main {
  public static void main(String[] args) {

    String str = "Radar", reverseStr = "";
    
    int strLength = str.length();

    for (int i = (strLength - 1); i >=0; --i) {
      reverseStr = reverseStr + str.charAt(i);
    }

    if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
      System.out.println(str + " is a Palindrome String.");
    }
    else {
      System.out.println(str + " is not a Palindrome String.");
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Radar is a Palindrome String.

#2 Code Example- Java Program to Check Palindrome Number

Code - Java Programming

class Main {
  public static void main(String[] args) {
    
    int num = 3553, reversedNum = 0, remainder;
    
    // store the number to originalNum
    int originalNum = num;
    
    // get the reverse of originalNum
    // store it in variable
    while (num != 0) {
      remainder = num % 10;
      reversedNum = reversedNum * 10 + remainder;
      num /= 10;
    }
    
    // check if reversedNum and originalNum are equal
    if (originalNum == reversedNum) {
      System.out.println(originalNum + " is Palindrome.");
    }
    else {
      System.out.println(originalNum + " is not Palindrome.");
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
3553 is Palindrome.
Advertisements

Demonstration


Java Programing Example to Check Palindrome-DevsEnv