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
- Initialize variable rev_num = 0, given a number num.
- 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
- 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
- Take the number num as input from the user.
- Create a function for reversing a number that accepts the input num as its parameter.
- Initialize the reverse number to 0.
- Check if num is greater than 0, and update the already existing reverse number by multiplying it by 10.
- And then add the reverse number with the last digit of the number num by taking modulo 10.
- Keep on calling the recursive function until num becomes less than 0.
- 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
- Change the given input N, to string num.
- Perform String slicing method in Python to reverse the string of number i.e. num[::-1].
- 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)
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.
Frequently Asked Questions
Q1. What is reverse() in Python?
Ans.The elements sorting order is reversed via the reverse() function.
Q2. What does reverse() return?
Ans. There is no value returned by the reverse() method. This is because it doesn’t create a new list or space; instead, it changes the old list immediately in place.
Q3. What is the rule of reverse?
Ans. In simple terms, you raise the power by one before dividing it by the power + 1. Keep in mind that the conditions n = 1 n=-1 n=1n, equals, minus, 1 are exceptions to this rule.
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