Given a number, reverse its digits and print the new number.
For example:
Input : num = 8972
Output: 2798
There are a couple of ways to solve this:
1. Reverse Number using String slicing
We convert the given number to string using str()
Reverse it using string slicing
Convert the reversed string to int and return the int.
defreverseNumber(n):reversed_number=int(str(n)[::-1])returnreversed_number
2. Reverse Number using While Loop
Using a while loop, iterate through the list of the digits from the last one and append the digits into a new number.
defreverseNumber(n):reversed_number=0while(n!=0):r=int(n%10)reversed_number=reversed_number*10+rn=int(n/10)returnreversed_number
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse