Friday, August 14, 2009

Palindrome Java Program

Hi friends, welcome back to Java Code Online. Today I will be discussing the Java Program to find out whether any entered String or number or any combination of them is a Palindrome or not.

A Palindrome is a string or number, that is exactly the same when it is read in the reverse order. That is for example "anna" is a palindrome, i.e. read the same either way.

The Java code for finding the Palindrome is given below:-

//////////////////////////////
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Palindrome {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("Enter a number or String to be checked for Palindrome");
InputStreamReader io = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(io);
String num = br.readLine();
StringBuffer num2 = new StringBuffer().append(num);
if(num.equals(num2.reverse().toString()))
{
System.out.println("It is a Palindrome");
}
else
{
System.out.println("It is not Palindrome");
}

}

}
/////////////////////////////

I hope the Java code provided for finding the Palindrome was helpful to you all. For more info on Java, keep checking Java Code Online.

1 comment:

  1. Hey, really smart funda applied. You converted the String to StringBuffer, so as to get the extra methods provided to it. Smart thinking. I liked your blog. You are already in my bookmarks.

    ReplyDelete

Note: Only a member of this blog may post a comment.