Python Program to Reverse a Number

Problem Description

Write a Python Program to reverse a given number.

Problem Solution

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.

Method 1: Reverse a Number in Python using While Loop

In this approach, we will divide the number by 10 and add the remainder to the sum.

Program/Source Code

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)
Program Explanation

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.

advertisement
advertisement

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.

Runtime Test Cases

Testcase 1: In this case, we are entering the number “124” as input.

Note: Join free Sanfoundry classes at Telegram or Youtube
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

Method 2: Reverse a Number in Python using Slice Operator

In this approach, we will use slice operator [::-1] to reverse the string.

advertisement
Program/Source Code

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)
Program Explanation

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.

advertisement
Runtime Test Cases

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

Method 3: Reverse a Number in Python using Recursion

In this approach, we will use a recursive function to calculate the reverse of the number.

Program/Source Code

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)
Program Explanation

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.

Program Output:

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.