In this post we will see How to Reverse Digits of a Given Number. Before that let’s know what we call reverse of a number.
Example: Let’s assume the number is 123456. Then the Reverse of the number will be 654321. We can write the program in many ways. But the easy way will be as in below steps.
Step 1: Take the Given Number and store it in a variable $inputNo.
Step 2: Loop the Given Number using while Loop and store the reverse number in a variable. Here the logic is to take last number and store it and then second last and append it with previous one and then rest of the digits.
Step 3: Print the Reverse of the number
Program:
inputNo = int(input("Enter a Number: "))
reversedNo = 0
while inputNo != 0:
digit = inputNo % 10
reversedNo = reversedNo * 10 + digit
inputNo //= 10
print("Reverse of the Number is : " + str(reversedNo))
Output:
Happy Coding!