Write a Python Program to reverse a given number.
Reversing a number means changing the order of its digits. For example, if we reverse the number 124, we get 421. It involves rearranging the digits of the given number from right to left.
There are several ways to reverse a number in Python language. Let’s take a detailed look at all the approaches to reverse a number in Python.
- Reverse a Number in Python using While Loop
- Reverse a Number in Python using Slice Operator
- Reverse a Number in Python using Recursion
In this approach, we will divide the number by 10 and add the remainder to the sum.
Here is the source code of the Python Program to reverse a given number.
n=int(input("Enter number: ")) rev=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 print("Reverse of the number:",rev)
1. User enters a number. Entered number is stored in the variable n.
2. A variable rev is initialized to 0, which will store the reversed number.
3. Using a while loop, the digits of the number are extracted and added to rev in reverse order.
4. The number is divided by 10 in each iteration to remove the last digit.
5. The reversed number is displayed as output.
Time Complexity: O(log n)
The time complexity of the program is O(log n), where n is the input number. This is because the number of iterations in the while loop is proportional to the number of digits in the input number.
Space Complexity: O(1)
The space complexity of the program is O(1) since it uses a constant amount of additional space to store the variables n, rev, and dig. The space required does not depend on the input size.
Testcase 1: In this case, we are entering the number “124” as input.
Enter number: 124 Reverse of the number: 421
Testcase 2: In this case, we are entering the number “4538” as input.
Enter number: 4538 Reverse of the number: 8354
In this approach, we will use slice operator [::-1] to reverse the string.
Here is the source code of the Python Program to reverse a given number using Slice Operator.
number = int(input("Enter a number: ")) reversed_number = int(str(number)[::-1]) print("Reversed number:", reversed_number)
1. The user is prompted to enter a number.
2. The input number is converted to a string using str(number).
3. The string representation of the number is reversed using slicing with [::-1].
4. The reversed string is converted back to an integer using int().
5. The reversed number is stored in the variable reversed_number.
6. The reversed number is printed as the output.
Time Complexity: O(n)
The time complexity of the program is O(n), where n is the number of digits in the input number. This is because the program iterates through each digit of the number once to reverse it.
Space Complexity: O(n)
The space complexity is also O(n) because the reversed_number variable stores the reversed number, which requires space proportional to the number of digits in the input number.
Testcase 1: In this case, we are entering the number “12345” as input.
Enter a number: 12345 Reversed number: 54321
Testcase 2: In this case, we are entering the number “4538” as input.
Enter a number: 4538 Reversed number: 8354
In this approach, we will use a recursive function to calculate the reverse of the number.
Here is the source code of the Python Program to reverse a given number using recursion.
def reverse_number(number): if number < 10: return number else: last_digit = number % 10 remaining_number = number // 10 reversed_number = reverse_number(remaining_number) return int(str(last_digit) + str(reversed_number)) number = int(input("Enter a number: ")) reversed_number = reverse_number(number) print("Reversed number:", reversed_number)
1. The function reverse_number takes a number as input and reverses it using recursion.
2. If the number is less than 10, it is a single-digit number, so we simply return the number itself.
3. Otherwise, we extract the last digit of the number using the modulus operator % and obtain the remaining number by integer division // by 10.
4. We recursively call the reverse_number function on the remaining number to reverse it.
5. Finally, we concatenate the last digit with the reversed remaining number and convert it back to an integer.
6. The reversed number is then printed.
Time Complexity: O(log N)
The time complexity of the program is O(log N), where N is the value of the input number.
Space Complexity: O(log N)
The space complexity is O(log N) as well, due to the recursion stack used in the function calls.
The program takes a number as input and reverses it using recursion. In this case, when the input number is 12345, the output is 54321, which is the reversed form of the input number.
Enter a number: 12345 Reversed number: 54321
To practice programs on every topic in Python, please visit “Programming Examples in Python”.
- Practice Programming MCQs
- Check Python Books
- Apply for Programming Internship
- Check Information Technology Books
- Apply for Python Internship