Palindrome Program in Java
An Introduction to the Palindrome and how Palindrome Program is used in Java is described using different approaches and gives solutions to the problems.
Join the DZone community and get the full member experience.
Join For FreeA Java interview will most likely involve a scenario in which candidates will have to demonstrate their logical and programming skills, as well as their experience, as part of their evaluation of the candidate. Among the most common questions asked in interviews is "Can you write a palindrome program in Java?" This is also one of the most challenging questions to answer. Palindromes are nothing more than numbers or strings that remain unchanged when reversed, while a non-palindrome is the opposite of it. It is obvious that when letters are reversed, they form mirror images of one another. Originally from the Greek language, palindromes are numbers, words, or sets of characters that are spelt the same way forward and backwards. Among the many examples of palindromes are 686, 1401042, 95359, 7007, Malayalam, Radar, MAAM, Radar, Level, etc. Upon reversing the letters, it is evident that they form mirror images of one another. With the remainder and division operators in Java, we can create a code that checks if a particular number is a palindrome or if it is not.
In this article, we will learn how to check whether the input number is a palindrome or a number that is not a palindrome. Here we will discuss what is palindrome, what is palindrome number, as well as a sample palindrome program in Java. The first step to understanding Java's implementation of palindromes involves understanding the concept of palindromes.
Here is a quick overview of what is Palindrome.
What Is Palindrome?
The word palindrome refers to words, phrases, numbers, or other sequences of characters that read the same whether read forward or backwards. If it were to be read forwards and backwards, it would have the same sequence of letters regardless of spacing, capitalization, or punctuation. In programming, an empty string can be considered a palindrome as well. It is therefore true that every single character string or one-digit number is considered to be a palindrome.
Example1: String
- Racecar
- Motor
- Tenet
Example2: Number
- 14541
- 6783876
- 232
What Is a Palindrome Number?
The palindrome number, also known as a numeric palindrome or a numeral palindrome, is a number that remains the same even when the digits in its number are reversed. 45654, for example, is a palindrome number with the same value when its digits are reversed. As you have probably guessed, if we reverse 1312, it is not a palindrome, since the reverse number is not the same as the original number.
Steps to Palindrome Number Program
- Input the number from the user or initialize it manually.
- Create a temporary variable and store the number in it.
- Reverse/Invert the number.
- Check the temporary number against the reversed number.
- If both the numbers are equal, print it is a palindrome
- Else, it is not a palindrome.
Palindrome Program in Java using FOR loop
Below is an easy-to-follow program that uses a "For Loop" to find a palindrome. In a for loop, digits in the input are iteratively checked until the input value becomes 0. Within the FOR loop, the modulus of the number (num) is taken into account, and it is saved in a variable called reverseNum
for every iteration of the loop. As a result, we are able to obtain the exact opposite/reverse of the input. Finally, the reversed number is compared with the original number in order to determine whether or not it is a palindrome. Here's an example that lets you check whether an input is a palindrome number.
Algorithm
- START the program
- Take user input or manually initialize it (num)
- Create a new variable (
initialNum
) and store the input. - Until num equals zero, find the remainder of num and store it in a variable (
reverseNum
). - Determine whether the
initialNum
equals thereverseNum
. - If both are equal,
- Print it is a palindrome
- Else, it is not a palindrome.
END the program
Code Snippet
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
int num= sc.nextInt();
int reverseNum=0, initialNum, remainder=0;
initialNum = num;
for(;num!=0;num/=10)
{
remainder= num % 10;
reverseNum = (reverseNum * 10) + remainder;
}
if (initialNum == reverseNum)
{
System.out.println("Yes, the given number " + initialNum + " is a palindrome.");
}
else
{
System.out.println("No, the given number " + initialNum + " is not a palindrome.");
}
}
}
Output 1:
Enter the number: 45354
Yes, the given number 45354 is a palindrome.
Output 2:
Enter the number: 61214
No, the given number 61214 is not a palindrome.
Palindrome Program in Java Using While Loops
Having explained the logic behind the code, let's now look at another possible way of writing the palindrome program in Java, which is by using a while loop. In a while loop, digits in the input are iteratively checked until the input value becomes 0. Within the while loop, the modulus of the number (num) is taken into account, and it is saved in a variable called reverseNum
for every iteration of the loop. Finally, the reversed number is compared with the original number in order to determine whether or not it is a palindrome. Here's an example that lets you check whether an input is a palindrome number.
Algorithm
- START the program
- Take user input or manually initialize it (num)
- Create a new variable (
initialNum
) and store the input. - Until num equals zero, find the remainder of num and store it in a variable (
reverseNum
). - Determine whether the
initialNum
equals thereverseNum
. - If both are equal,
- Print it is a palindrome
- Else, it is not a palindrome.
- END the program
Code Snippet
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
int num= sc.nextInt();
int reverseNum=0, initialNum, remainder;
initialNum = num;
while(num!=0)
{
remainder= num % 10;
reverseNum = (reverseNum * 10) + remainder;
num = num / 10;
}
if (initialNum == reverseNum)
{
System.out.println("Yes, the given number " + initialNum + " is a palindrome.");
}
else
{
System.out.println("No, the given number " + initialNum + " is not a palindrome.");
}
}
}
Output 1:
Enter the number: 98989
Yes, the given number 98989 is a palindrome.
Output 2:
Enter the number: 3624251
No, the given number 3624251 is not a palindrome.
Conclusion
Having learned what a palindrome is and how to write code for palindromes in Java, we have reached the end of this article. Hopefully, all the information that has been shared with you in this tutorial has clarified any doubt you might have had. The most important thing you can do is to practice as much as possible and put what you have learned into practice. The number is called a palindrome number if it consists of just one letter or no letters. When the reverse of a number is the same as its original, then it is called a palindrome string. Hopefully, this helps you enhance your Java skills.
Opinions expressed by DZone contributors are their own.
Comments