array palindrome java code example
Example 1: generate palindrome java
public static String generatePalindrom(String s) {
return s.equals(reverseString(s)) ? s
: generatePalindrom(String.valueOf(Integer.parseInt(s) + Integer.parseInt(reverseString(s))));
}
public static String reverseString(String s) {
return s.length() == 1 ? s : s.charAt(s.length() - 1) + reverseString(s.substring(0, s.length() - 1));
}
Example 2: palindrome number in java
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
int num,temp,reverse=0;
Scanner input=new Scanner(System.in);
num=in.nextInt();
temp=num;
while(temp!=0)
{
int d=temp%10;
reverse=reverse*10+d;
temp/=10;
}
if(reverse==num)
{
System.out.println("Number is palindrome");
}
else
{
System.out.println("Number is not palindrome");
}
}
}