InfyTQ Python Coding Question & Answer 2022
Identify
Palindrome
Problem
Statement-:
For a given positive number num, identify the
palindrome formed by performing the following operations-
Add num and its reverse
Check whether the sum is palindrome or not. If not,
add the sum and its reverse and repeat the process until a palindrome is
obtained
For example:
If original integer is 195, we get 9,339 as the
resulting palindrome after the fourth addition:
195
+ 591
————–
786
+ 687
————–
1473
+ 3741
————–
5214
+ 4125
———-
9339
Input
format:
Read num from the standard input stream.
Output
format:
Print the palindrome calculated to the standard
output stream.
Sample
Test Cases
Sample
Input 1
124
Sample
Output 1
545
Explanation
1
The
sum of 124 and its reverse 421 is 545 which is a palindrome.
Sample
input 1
4
Sample
output 1
8
Explanation
1
The
sum of 4 and its reverse 4 is 8 which is a palindrome.
Program :
def
isPalindrome(n):
return str(n)[::-1]==str(n)
def
rev(n):
return int(str(n)[::-1])
n=int(input("Number
: "))
while(1):
n=n+rev(n)
if(isPalindrome(n)):
print(n)
break
Comments
Post a Comment