You might have heard about the word Palindrome in your school. In the realm of computer programming and technological advancements, everything is programmed to offer high-class services to humans with a single touch. A palindrome number or string can also be identified through programming in Java. A palindrome number/string is simply a number or word whose sequence reads the same backwards as it reads from forward. Java is one of the widely acclaimed programming languages taught in schools and at higher levels of education. You can use this platform to analyze whether the input is palindrome or not. Stay tuned and continue reading this blog to learn about the Palindrome program in Java!
Table of Contents
Also Read: Know all about Computer Programming
What is a Palindrome Number?
A palindrome number reads the same from forward and backward (punctuation, space, and capitalization need to be ignored). That means that the palindrome number is the number that remains the same even after reversing the number.
You can understand this through the following examples of Palindrome Numbers:
- 121
- 101
- 131
- 1221
- 1661
When you reverse these numbers you will find that they will remain the same so such numbers are referred to as Palindrome.
Also Read: Diploma in Computer Applications
Steps to Check the Palindrome
Here are some basic steps that the programmer needs to follow:Source: SDET-QA
- Ask the user to Input the number/string
- Reverse the entered number/string
- Compare the input
- Generate the Output
Palindrome Program in Java to Check Whether the Input Number is Palindrome or Not
Here is a Palindrome Program in Java:
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
int number = scanner.nextInt();
int originalNumber = number;
int reversedNumber = 0;
while (number > 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
if (originalNumber == reversedNumber) {
System.out.println(originalNumber + ” is a palindrome”);
} else {
System.out.println(originalNumber + ” is not a palindrome”);
}
}
}
Output
Enter a number: 121
121 is a palindrome
What is a Palindrome String?
Palindrome String refers to the string that is the same after reversing.
Here are some examples:
- noon
- mom
- nitin
- racecar
- madam
- kanak
- refer
- rotor
- taco cat
- kayak
- level
- Civic
Also Read: Python Vs Java
Palindrome Program in Java to check a String
Given below a Java program to check whether the entered string is a palindrome or not:
public class Palindrome
{
public static void main(String args[])
{
String x, y = “”;
Scanner a = new Scanner(System.in);
System.out.print(“Enter string you want to check:”);
x = a.nextLine();
int l = x.length();
for(int k = l – 1; k >= 0; k–)
{
y = y + x.charAt(k);
}
if(x.equalsIgnoreCase(y))
{
System.out.println(“The string is palindrome.”);
}
else
{
System. out.println(“The string is not a palindrome.”);
}
}
}
Output
Enter string you want to check:
Tattarrattat
The String is palindrome.
Relevant Blogs
FAQs
A palindrome number refers to the number that remains the same after reversing. This means if we reverse all the digits of the entered number the number remains unchanged. Example of palindrome number in Java is 121, 131, 1331, 1441, etc.
The concept of Palindrome number is also applicable to Palindrome string. It is the string of letters that remains the same even after reversing.
Various method to check Palindrome in Java is the Scanner class (to obtain user input), Recursion, For loop, While loop, and If – else statements.
For more information about such informative articles, check the trending events page of Leverage Edu.