Priya R Priya R
Updated date Nov 13, 2023
In this blog, we will learn how to reverse a number in Python using various methods. This blog provides step-by-step explanations, code examples, and outputs for each method.

Introduction:

Reversing a number is a simple task in programming, and Python offers multiple methods to accomplish this. In this blog, we will explore several techniques to reverse a number, providing detailed explanations, Python code examples, and the corresponding output for each method. 

Method 1: Using String Reversal

The first method to reverse a number involves converting it into a string and then reversing the string. Here's the Python code for this approach:

def reverse_number_method1(number):
    # Convert the number to a string
    num_str = str(number)
    
    # Reverse the string
    reversed_str = num_str[::-1]
    
    # Convert the reversed string back to an integer
    reversed_num = int(reversed_str)
    
    return reversed_num

Output:

Let's reverse the number 12345 using this method. The output will be:

original_number = 12345
reversed_number = 54321

In this method, we first convert the number to a string, then use string slicing to reverse it, and finally convert it back to an integer.

Method 2: Using Arithmetic Operations

The second method involves using arithmetic operations to reverse a number. Here's the Python code for this approach:

def reverse_number_method2(number):
    reversed_num = 0
    
    while number > 0:
        digit = number % 10
        reversed_num = (reversed_num * 10) + digit
        number //= 10
    
    return reversed_num

Output:

Let's reverse the number 9876 using this method. The output will be:

original_number = 9876
reversed_number = 6789

In this method, we use a while loop to extract the last digit of the number and build the reversed number by continuously multiplying it by 10 and adding the extracted digit.

Method 3: Using Recursion

The third method involves using recursion to reverse a number. Here's the Python code for this approach:

def reverse_number_method3(number):
    if number < 10:
        return number
    
    last_digit = number % 10
    remaining_digits = number // 10
    
    return (reverse_number_method3(remaining_digits) * 10) + last_digit

Output:

Let's reverse the number 456 using this method. The output will be:

original_number = 456
reversed_number = 654

In this method, we break the number into its last digit and the remaining digits using recursion and then reverse it.

Method 4: Using a Stack

The fourth method involves using a stack to reverse a number. Here's the Python code for this approach:

def reverse_number_method4(number):
    stack = []
    
    while number > 0:
        digit = number % 10
        stack.append(digit)
        number //= 10
    
    reversed_num = 0
    multiplier = 1
    
    while stack:
        reversed_num += stack.pop() * multiplier
        multiplier *= 10
    
    return reversed_num

Output:

Let's reverse the number 123 using this method. The output will be:

original_number = 123
reversed_number = 321

In this method, we use a stack data structure to store the digits of the number and then pop them to construct the reversed number.

Method 5: Using List Reversal

The fifth method involves converting the number to a list of digits, reversing the list, and then converting it back to a number. Here's the Python code for this approach:

def reverse_number_method5(number):
    digits = []
    
    while number > 0:
        digit = number % 10
        digits.append(digit)
        number //= 10
    
    reversed_num = 0
    multiplier = 1
    
    for digit in digits[::-1]:
        reversed_num += digit * multiplier
        multiplier *= 10
    
    return reversed_num

Output:

Let's reverse the number 7890 using this method. The output will be:

original_number = 7890
reversed_number = 987

In this method, we first convert the number to a list of digits, reverse the list, and then convert it back to a number.

Conclusion:

In this blog, we have explored five different methods to reverse a number in Python, Method 1 involves converting the number to a string, reversing the string, and converting it back to an integer. This method is simple and intuitive but may not be the most efficient for large numbers. Method 2 uses arithmetic operations and a while loop to reverse the number. It is efficient and suitable for reversing numbers of any size. Method 3 employs recursion to reverse the number, which can be elegant but may not be the most efficient approach for very large numbers. Method 4 utilizes a stack data structure to store and reverse the digits of the number. This method is flexible and can handle various input sizes. Method 5 converts the number to a list of digits, reverses the list, and converts it back to a number. It offers flexibility but involves more data conversions.

Comments (0)

There are no comments. Be the first to comment!!!