  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Python Program to Reverse a Number

Last Updated on May 11, 2023 by Prepbytes Reversing a number refers to reordering the digits such that the first digit is at the end and the last digit comes first, followed by the second last digit, and so on. The next section will go through several different ways to reverse a number in Python.

## Algorithm using While loop

1. Initialize variable rev_num = 0, given a number num.
2. Run a Loop while num > 0
• Multiply rev_num by 10 and add remainder of number
• divide by 10 to rev_num
• rev_num = rev_num*10 + num%10;
• Divide num by 10
3. At last return rev_num.

### Code Implementation to Reverse a Number using While Loop

```number = int(input("Enter the integer number: "))

revs_number = 0

while (number > 0):
remainder = number % 10
revs_number = (revs_number * 10) + remainder
number = number // 10

print("The reverse number is :",revs_number)
```

Time Complexity: O(log(n)) will be the time complexity for reversing the number where n is the input number.

Space Complexity: O(1) will be the space complexity as there is no extra space used in reversing the number.

## Algorithm to Reverse a Number using Recursion

1. Take the number num as input from the user.
2. Create a function for reversing a number that accepts the input num as its parameter.
3. Initialize the reverse number to 0.
4. Check if num is greater than 0, and update the already existing reverse number by multiplying it by 10.
5. And then add the reverse number with the last digit of the number num by taking modulo 10.
6. Keep on calling the recursive function until num becomes less than 0.
7. Finally, return the reverse number and print it as output.

### Code Implementation to Reverse a Number using Recursion

```revnum = 0
base_pos = 1;

def reverse(num):
global revnum
global base_pos
if(num > 0):
reverse((int)(num / 10))
revnum += (num % 10) * base_pos
base_pos *= 10
return revnum

num = 12345
print(reverse(num))
```

Time Complexity: Time complexity for reversing the number O(logN).

## Algorithm to Reverse a Number using Slicing Method of String

1. Change the given input N, to string num.
2. Perform String slicing method in Python to reverse the string of number i.e. num[::-1].
3. Print the result after the string slicing method.

### Code Implementation to Reverse a Number using Slicing Method of String

```n = int(input())
num = str(n)
result = int(num[::-1])
print(result)
```

Conclusion
This blog has provided details about several ways to reverse a number in Python. Reversing a number will aid in logic building, and sound logic will strengthen your programming endeavors. Many service-based organizations that hire new students such as TCS, Wipro, Accenture, and Capgemini, have interview questions that involve reversing numbers.