Menu Close

Find next Palindrome of N number

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 continues integer t, the number of test cases and followed by t lines containing integers K.

Output:
For each K, output the smallest palindrome larger than K.

T=int(input("Enter the number of test cases: "))

while(T!=0):
	K=int(input("Enter a positive integer: "))
	for i in range((K+1),1000000):
		digit=str(i)
		Rtemp=int(''.join(reversed(digit)))
		if Rtemp==i:
			print("The smallest palindrom larger than {} is {}\n".format(K,i)) #Basically the next palindrome number after K
			break
	T-=1

Output:
Enter the number of test cases: 2

Enter a positive integer: 808
The smallest palindrome larger than 808 is 818

Enter a positive integer: 2133
The smallest palindrome larger than 2133 is 2222


More Codes to Fcuk