Palindrome Number in Java
A Palindrome number or word which will read the same from either side, backward or forward.
For example, 202 or madam read the same from both forward and backward.
Java Program for Palindrome Number
import java.io.*;
public class PalindromeNumber
{
public static void main(String args[])throws IOException
{
int n, rev, a ,m;
rev=0;
InputStreamReader ab = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ab);
System.out.print("Enter number to Palindrome:");
n = Integer.parseInt(br.readLine());
m = n;
// Reversing the number
while(n != 0)
{
a = n % 10;
rev = rev * 10 + a;
n = n / 10;
}
System.out.println("Reverse Number:" + rev);
// Checking Palindrome or not
if(m == rev)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}
Output
Enter number to Palindrome:202
Reverse Number:202
Palindrome
Enter number to Palindrome:221
Reverse Number:122
Not Palindrome
Java Program for Palindrome Word or String
For String Palindrome
import java.util.Scanner;
/**
* class Palindrome Word here.
* Khurshid Md Anwar (InspireSkills)
*/
public class PalindromeWord
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String st; // Variable for Word taken
System.out.println("Enter a string or word ");
st=sc.nextLine();
int i,l;
char ch;
String revWord=""; // for reverse Word
l = st.length(); // Finding the length of word
for(i=0;i<l;i++)
{
ch = st.charAt(i);
revWord = ch + revWord;
}
// Checking Plaindrome or not
if(st.equals(revWord))
{
System.out.println("Palindrome Word");
}
else
{
System.out.println("Not a Palindrome Word");
}
}
}
Output
Enter a string or word
madam
Palindrome Word
Enter a string or word
hello
Not a Palindrome Word
Visit for More Program