Skip to main content

Program to accept a number. Display the reverse of the number. Using Python

Program to accept a number.
Display the reverse of the number.
Input: 123
Output: 321

r=0

n = int(input("Enter a number:"))

while n>0:

    d=n%10

    r=r*10+d

    n=n//10

print("Reverse of the number is",r)


'''

Dry Run

r=0

n=123


123>0,True, d=123%10=3, r=0*10+3=3, n=123//10=12

12>0,True, d=12%10=2, r=3*10+2=32, n=12//10=1

1>0,True, d=1%10=1, r=32*10+1=321, n=1//10=0

0>0,False

Reverse of the number is 321

'''


Popular posts from this blog