-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome.py
44 lines (42 loc) · 1.29 KB
/
palindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from os import system
system("cls")
print("To find the next palindrome number!\n")
try:
n_s = input('Enter any palindrome number : ')
n = int(n_s)
l=len(n_s)
if l%2==0:
p2 = int(l)-1
cl= int(l/2) if l!=1 else int(l/2)+1
else:
p2 = int(len(n_s))-1
cl= int(l/2+0.5) if l!=1 else int(l/2)+1
palindrome = True
index=None
for i in range(cl):
if int(n_s[i])!=int(n_s[p2]):
print("Given number is not palindrome number!")
palindrome = False
break
else:
p2-=1
index=i
if palindrome:
print(f"\nGiven number is palindrome")
next_p=''
if l%2==0:
for i in range(int(l/2)):
next_p+=n_s[i]
next_p=str(int(next_p)+1)
next_p+=next_p[::-1]
else:
for i in range(int(l/2+0.5) if l!=1 else 1):
next_p+=n_s[i]
next_p = str(int(next_p)+1)
next_p += next_p[:index][::-1]
print(f"The next palindrome number to that of the given palindrome will be:\n{next_p}")
except:
if n_s=='':
print("\nPlease give some input!\n")
else:
print("\nSomething went wrong, try again!\n")