Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reversing and adding numbers. You will write a Python program (called q2.py) to

ID: 3922645 • Letter: R

Question

Reversing and adding numbers. You will write a Python program (called q2.py) to implement the following procedure to produce palindromic numbers. a) Take any positive integer x. If x is a palindrome, stop. Otherwise, continue to step (b). b) Reverse the digits of x. c) Add the reverse to x. d) If the sum is a palindrome, then stop. Otherwise, let x represent the sum, and repeat steps (b) through (d). Steps (b) through (d) represent a single iteration. Below, are the number of iterations required for a few numbers to become palindromic using the reverse and add procedure. 8 is a palindrome. It is palindromic after 0 iterations. 56 becomes palindromic after 1 iteration: 56 + 65 = 121. 57 becomes palindromic after 2 iterations: 57 + 75 = 132, 132 + 231 = 363. 59 becomes palindromic after 3 iterations: 59 + 95 = 154, 154 + 451 = 605, 605 + 506 = 1111. 89 takes an unusually large 24 iterations (the most of any number under 10,000 that is known to resolve into a palindrome) to reach the palindrome 8813200023188. For 196, it is not known if the above procedure leads to a palindromic number.

Explanation / Answer

In the beginning call inputN function:--

>>inputN()

def Reverse_Number(n):

m=n
a = 0
while(m!=0):
a = m % 10 + a * 10
m = m / 10
return a

def Palindrome_Number(n):

m=n
a = 0
flag=0
while(m!=0):
a = m % 10 + a * 10
m = m / 10
if( n == a):
flag=1
return flag

def inputN():
n=input('enter n :');
var=1

count=0
while var==1:
if Palindrome_Number(n)==1:
print n,'is palindrome after ',count,'iterations'
break
else:
rev=Reverse_Number(n)
print 'reverse of',n,'is ',r ev
print n,'+',rev,'=',n+rev
n=n+rev

count=count+1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote