Palindrome in Java

Table of Contents

Compile and Run the following code with Command-Prompt Or download Notepad++

For Compilation javac Sample.java

For Execution java Sample

import java.util.Scanner;
class Sample
{
  public static void main(String args[])
  {
    String a,b;
    StringBuffer sb;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the string to be checked: ");
    a = sc.nextLine();
    sb = new StringBuffer(a);
    b = sb.reverse().toString();
    if(a.equals(b))
      System.out.println("Entered String is a Palindrome");
    else
      System.out.println("Entered String is not a Palindrome");
  }
}
Previous Fibonacci Series in Java
Next Perfect Number in Java

Leave a Reply