package com.journaldev.util;

public class Palindrome {

public static boolean isPalindrome(String str) {

if (str == null)

return false;

StringBuilder strBuilder = new StringBuilder(str);

strBuilder.reverse();

return strBuilder.toString().equals(str);

}

public static void main(String[] args) {

System.out.println(“Is aba palindrome? ” + Palindrome.isPalindrome(“aba”));

System.out.println(“Is abc palindrome? ” + Palindrome.isPalindrome(“abc”));}}

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

do {

Scanner sc = new Scanner(System.in);

System.out.println(“Enter the string: “);

String str = sc.nextLine();

StringBuffer sb = new StringBuffer(str);

sb.reverse();

if (str.equalsIgnoreCase(sb.toString())) {

System.out.println(“Entered string is a palindrome”);

} else {

System.out.println(“Entered string is not a palindrome”);

}

} while (askAgain());

}

private static boolean askAgain() {

Scanner sc = new Scanner(System.in);

System.out.println(“Do you want to check another palindrome? Yes/No”);

String answer = sc.nextLine();

if (answer != null && answer.length() > 0) {

if (answer == “Yes” || answer == “yes”) {

return true; // answer

package palindrome;

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print(“Enter a word: “);

String word = input.nextLine();

int low = 0; //starting point of the string

int high = word.length() – 1; //ending point of the string

boolean isPalindrome = true; // to check if it is a palindrome or not

while (low < high) { if (word.charAt(low) != word.charAt(high)) { //check each character from starting and ending and compare them together isPalindrome = false; //if the characters are not same then its not a palindrome break;// break out of the loop as soon as we know it is not a palindrome } low++; //incrementing starting point by one till it becomes equal to ending point import java.io.*; class Palindrome { public static void main(String args[])throws IOException { int rev=0,rem,n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number:"); n=Integer.parseInt(br.readLine()); int temp=n; while(n>0)

{

rem=n%10;

rev=rev*10+rem;

n/=10;// n=n/10 also correct.

}

if(temp==rev) // n is redundant as temp stores the initial value of n that we assigned in line 6 and rev is the reverse of this value. You can use any of them here and it will work fine. I used temp because it is the original name of the variable used in line 6 and I didn’t want to confuse you by changing it’s name here! If you find any mistake in my code please do inform me so that I can make necessary changes in my code. Please use Beautiful Code plugin if you find any errors other than logical errors! Thanks! 🙂

public class Palindrome {

public static void main(String[] args) {

int num = 121, reversedInteger = 0, remainder, originalInteger;

originalInteger = num;

// reversed integer is stored in variable

while( num != 0 )

{

remainder = num % 10;

reversedInteger = reversedInteger * 10 + remainder;

num /= 10;

}

// palindrome if orignalInteger and reversedInteger are equal

if (originalInteger == reversedInteger)

System.out.println(originalInteger + ” is a palindrome.”);

else

System.out.println(originalInteger + ” is not a palindrome.”);

}

}

import java.util.*;

public class Palindrome {

public static void main(String[] args) {

System.out.println(“Enter a string”);

Scanner input=new Scanner(System.in);

String str=input.nextLine();

int i,len,flag=0;

len=str.length();

for (i=0;i

Tomy

Tomy is a contributor at AskMeCode. We are committed to providing well-researched, accurate, and valuable content to our readers.

You May Also Like

Does Clean Code Matter? This Simulator Shows You Why It Does

Does Clean Code Matter? This Simulator Shows You Why It Does

Just like the title of this article suggests, this post is about the importance of clean code. You will learn...

honeycode at Github

honeycode at Github

honeycode at Github: A blog on github with code samples. I will be posting a series of articles highlighting all...

An Introduction to AI in Your Code with TensorFlow on Google Cloud Platform

An Introduction to AI in Your Code with TensorFlow on Google Cloud Platform

An Introduction to AI in Your Code with TensorFlow on Google Cloud Platform: A blog describing the basics of AI....

Benefits of a Personal Website

Benefits of a Personal Website

I got my first job after responding to a tweet that was looking for a junior developer. I had no...

Leave a Reply

About | Contact | Privacy Policy | Terms of Service | Disclaimer | Cookie Policy
© 2026 AskMeCode. All rights reserved.