Question:
Write a program in python to find the reverse of given number.
Answer:
num=int(input("Enter the number:")) # At this point we have taken an integer value from console.
rev=(rev*10+num%10)
# At this point we are finding the last number of integer stored in the "num" and that last number is added in previous 10th place of "rev"(rev*10).
num=num//10
# At this point last digit will be removed from number. "//" removed remainder from the number .
code
---------------------------------------------------
num=int(input("Enter the number:"))rev=0
while(num>0):
rev=(rev*10+num%10)
num=num//10
print("reverse =",rev)
---------------------------------------------------
Output is:
Enter the number:98765123
reverse= 32156789
reverse= 32156789
********************************************
Above code can also be write as below in simplest way:
---------------------------------------------------------
num=int(input("Enter the number:"))
reverse = 0
remainder = 0
while(num>0):
remainder = num%10
reverse = reverse*10 + remainder
num = num//10
print("reverse=",reverse)
reverse = 0
remainder = 0
while(num>0):
remainder = num%10
reverse = reverse*10 + remainder
num = num//10
print("reverse=",reverse)
---------------------------------------------------------
Note:
you could run the above commands on online compiler. go to the below link and copy pest the code there and play with code to understand.
Link: https://www.programiz.com/python-programming/online-compiler/
No comments:
Post a Comment