Mar-09-2017, 10:56 AM
If you for some reason do not want to use Larz60+ suggestion, you can do it directly by "removing" either first or last digit of the input number and use it to construct new "reversed" number.
And if your only problem is to check whether number is a palindrome, then simple check based on Larz60+ code is enough:
num = 12345 rev_num = 0 while num: rev_num = 10 * rev_num + num % 10 # extracts last digit of num and "appends" to "shifted" rev_num num //= 10 # just shortcut for num = num // 10 - removes "used" digit print("num = {}, reversed num = {}".format(num, rev_num))
Output:num = 0, reversed num = 54321
As you can see, it destroys the input number in a process of reverting it. If you need to keep your number, you need to use other variable (or you can put that reverting code into a function).And if your only problem is to check whether number is a palindrome, then simple check based on Larz60+ code is enough:
str(num) == str(num)[::-1]