Palindrome program in java examples



Sample program to find whether the String is palindrome or not

public class PalindromeCheck {

public static void main(String args[])throws IOException

{

// user entered string is reading using BufferedReader

   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

   String s1,s2="";

    int i,l;

   char ch;

   System.out.println("ENTER STRING TO CHECK");

   s1=in.readLine();

   l=s1.length();

   for(i=l-1;i>=0;i--)

{

ch=s1.charAt(i);

s2=ch+s2;

}

if(s1.equals(s2))

     System.out.println("PALINDROME");

else

             System.out.println("nOT PALINDROME");

}



output:


ENTER STRING TO CHECK

ram

PALINDROME


2.Sample program to find whether the number is palindrome or not

public class PalindromeNumberCheck {

public static void main(String[] args) {

int num = 121;

int res = num;

int reverseno = 0;

while (res != 0) {

int rem = res % 10;

reverseno = reverseno * 10 + rem;

res = res / 10;

}

//COMAPRING ENTERED NO AND REVERSE NO

if (num == reverseno) {

System.out.println("NO  IS PALINDROME");

} else {

System.out.println("NO IS NOT PALINDROME");

}

}

}

output:

NO  IS PALINDROME


Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: