Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12829 Members
Join this group to post and comment.
sookie • Aug 24, 2009

# Program to find out Next Palindrome

Hi all programmers here!

Problem Statement :
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input

The first line contains integer t, the number of test cases. Followed by t lines containing integers K.
Output

For each K, output the smallest palindrome larger than K.
Example

Input:
2
808
2133

Output:
818
2222
Warning: large Input/Output data, be careful with certain languages

Source: Code Chef

For above Problem, I created following program and also even submitted at the site mentioned in Source but their machine is again and again giving some Runtime error. As per those folks, it is because I am doing some wrong in I/O handling but as per me , it is working fine in my machine. I am not able to trace it out. 😔 Anyone here any ideas what error could be?

My Program:
package code.chef;

import java.io.IOException;

/**
*
* @author sookie
*/
public class NextPalindrome {
public static void main(String[] args) throws IOException {

int t, k;
StringBuilder z;

String inputs[] = new String[t];
for(int i = 0; i < t; i++) {
}
for (int i = 0; i < t; i++) {

k=Integer.parseInt(inputs[i]);
do{
z=new StringBuilder();
k=k+1;

z.append(k);
z=z.reverse();

if(Integer.parseInt(z.toString())==k){
System.out.println(k);
break;
}else continue;
}while(k>=1);

}
}

}

Output:
2
808
2222
818
2332
Thanks in advance for finding out at what case, it can give runtime error !