palindrome java method code example
Example 1: Java program to check whether string is palindrome using library methods
public class StringPalindromeJava
{
public static void isPalindrome(String str)
{
String strReverse = new StringBuffer(str).reverse().toString();
if(str.equals(strReverse))
{
System.out.println(str + " is palindrome string.");
}
else
{
System.out.println(str + " is not palindrome string.");
}
}
public static void main(String[] args)
{
isPalindrome("eye");
isPalindrome("rotator");
}
}
Example 2: palindrome in java
package com.candidjava; import java.util.Scanner; public class PalindromeUptoN{ public static void main(String[] args) { int n, b, rev = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter the Palindrome No N:"); int N = sc.nextInt(); System.out.print("Palindrome numbers from 1 to N:"); for (int i = 1; i <= N; i++) { n = i; while (n > 0) { b = n % 10; rev = rev * 10 + b; n = n / 10; } if (rev == i) { System.out.print(i + " "); } rev = 0; } } }