Java Program for String Palindrome
Updated on Mar 28, 2025 | 4 min read | 5.93K+ views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 4 min read | 5.93K+ views
Share:
When a number remains the same even after reversal, it is referred to as a palindrome. Some examples of palindromes include 656, 232, 46764, and the like. Palindromes can also exist as strings such as MADAM (Palindrome Program in Java , n.d.). In Java, a basic algorithm may be applied to check whether a number is a palindrome. The basic steps included in the algorithm are summarized below:
Check out our free technology courses to get an edge over the competition.
Several other approaches may be applied to check whether a given number or string is a palindrome. In the example below, two pointers are used to navigate from the beginning to the end of the input provided. The program confirms whether the supplied input is a palindrome (Java program to check whether a string is a Palindrome, 2019).
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!
The code to check string palindrome in Java is as follows:
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.");
}
}
}
Output:
Radar is a Palindrome String.
In this example, the “Radar” string has been stored in str. The following has been used for the code:
The toLowerCase() method can transform both reverseStr and str to lowercase. It happens because Java is extremely case-sensitive. Therefore, R and r are two different values in Java.
The equals() method can determine whether there’s equality between two strings.
The code to check the string palindrome program in Java is as follows:
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
// estore 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.");
}
}
}
Output:
3553 is Palindrome.
In this example, the number 3533 is stored in num and originalNum variables. The following has been used here:
To write the palindrome number program in Java, the following algorithm will be followed:
900 articles published
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology s...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources