Python Program to Reverse a Number

In this blog, we have to reverse a number in Python. We will go through different approaches for finding the python program to reverse a number. Reversing a number will help in improving the problem solving and logic building skills. Many difficult problems require a number to reverse and by this blog, you will be a master in those problems.

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

With the help of this blog, we have discussed multiple approaches to reverse a number in python. Reversing a number will help in building the logic and having good logics will make your coding journey strong. For freshers, many service based companies like TCS, Wipro, Accenture and Capgemini have asked questions like reversing a number in their interview.

Other Python Programs
Python program to add two numbers
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to find the middle of a linked list using only one traversal
Python program to reverse a linked list

Leave a Reply

Your email address will not be published. Required fields are marked *