class NextPallindrome {
public static void main
(String[] argc
){ int num= 8646;
System.
out.
println(nextPanlindrome
(num
)); }
/**
* Get next pallindrome
* @param num
* @return
*/
public static int nextPanlindrome(int num) {
if(isPalindrome(num))
num++;
int end=temp.length() -1;
int base=1;
for(int start = 0; start < end; start++, end--)
{
// Gives the right digit
int right
= Integer.
parseInt(temp.
charAt(end
)+"") * base
;
// Gives the left digit
int left
= Integer.
parseInt(temp.
charAt(start
)+"") * base
;
// add differnce to the number
num = num + left - right ; //------(1)
base *=10;
if(right > left)
{
num += base; // for incresing the value of number which got decreased at (1)
// previous step indroduces asymmetry if its a even number.
if(start == end - 1) // For even numbers eg. case 8468 (adjacent digits)
num += base/10;
}
temp = num + "";
}
return num;
}
public static boolean isPalindrome(int num) {
int reverse = 0, temp=num;
while( temp != 0 ) {
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
return (num == reverse ? true: false);
}
}
IGNsYXNzIE5leHRQYWxsaW5kcm9tZSB7CgoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJnYyl7CgkJaW50IG51bT0gODY0NjsKICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihuZXh0UGFubGluZHJvbWUobnVtKSk7Cgl9CgoJLyoqCgkgKiBHZXQgbmV4dCBwYWxsaW5kcm9tZQoJICogQHBhcmFtIG51bQoJICogQHJldHVybgoJICovCglwdWJsaWMgc3RhdGljIGludCBuZXh0UGFubGluZHJvbWUoaW50IG51bSkgewoKCQlpZihpc1BhbGluZHJvbWUobnVtKSkKCQkJbnVtKys7CgoJCVN0cmluZyB0ZW1wID0gbnVtICsgIiI7CgkJaW50IGVuZD10ZW1wLmxlbmd0aCgpIC0xOwoJCWludCBiYXNlPTE7CgoJCWZvcihpbnQgc3RhcnQgPSAwOyBzdGFydCA8IGVuZDsgc3RhcnQrKywgZW5kLS0pCgkJewoKCQkJLy8gR2l2ZXMgdGhlIHJpZ2h0IGRpZ2l0CgkJCWludCByaWdod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