Python Program to Find the Reverse of a Number

reverse of a number

Python program to find the reverse of a number has been shown here. For example, if a number is 12345 then the reverse of the number is 54321. The following section covers both of the iterative and recursive approaches to find the reverse of a number. The algorithm, pseudocode and time complexity of the program have been shown below.






1. Algorithm to find reverse of a number


1. Take a number n as input.

2. Initialize a variable to 0 say reverse = 0.

3. Perform r = n % 10.

4. Perform reverse = reverse * 10 + r.

5. Perform n = n / 10

6. If n = 0 stop the process and display reverse as output else go to step 3.




2. Pseudocode to find reverse of a number


Input : A number $n$

Output : The reverse of the number $n$

1. Procedure reverse($n$):

2. $reverse \leftarrow 0$

3. Repeat until $n != 0$

4. $reverse \leftarrow reverse * 10 + (n \mod 10)$

5. $n \leftarrow n / 10$

6. Return $reverse$

7. End Procedure





3. Time complexity to find reverse of a number


Time Complexity: O(log(n))

Where n is the input number.





4. Program & output to find reverse of a number




4.1. Python Program & output to find reverse of a number using iteration

Code has been copied
####################################
#       alphabetacoder.com
# Python program to find the reverse
# of a number using iteration
####################################

# take input of the number
num = int(input("Enter the number = "))

# initialize
revnum = 0

# find the reverse
while num != 0:
    # extract the unit digit
    r = num % 10
    # store the reverse number.
    # provide appropriate positional value
    # of each digit in each iteration.
    revnum = revnum * 10 + r
    # divide the number by 10
    num = num // 10

# display result
print("Reverse of the number =", revnum)

Output


Enter the number = 6894562

Reverse of the number = 2654986



    The given Python program finds the reverse of a number using iteration. Here's how it works:
  1. It takes input from the user for the number to be reversed, using the input() function and int() function to convert the input to an integer. The entered number is stored in the variable num.
  2. The variable revnum is initialized to 0. This variable will store the reversed number.
  3. The program enters a while loop that continues until num becomes 0.
  4. In each iteration of the loop:
    • The unit digit of num is extracted using the modulus operator % with 10. This gives the remainder when num is divided by 10, which is the unit digit.
    • The extracted digit r is added to revnum after multiplying it by 10 to shift the existing digits in revnum to the left and make room for the new digit.
    • The value of num is updated by integer division // with 10, which removes the unit digit.
  5. Once the loop ends, the reversed number is stored in revnum.
  6. Finally, the program prints the reversed number using the print() function.



4.2. Python Program & output to find reverse of a number using recursion

Code has been copied
####################################
#       alphabetacoder.com
# Python program to find the reverse
# of a number using recursion
####################################

# recursive function to reverse a number
def reverse(num, rev):
    if num == 0:
        return rev
    else:
        return reverse(num // 10, (rev * 10 + num % 10))


# take input of the number
num = int(input("Enter the number = "))

# display result by calling function
print("Reverse of the number =", reverse(num, 0))

Output


Enter the number = 56

Reverse of the number = 65



    The given code is a Python program that finds the reverse of a number using recursion. Here's how it works:
  1. The code defines a recursive function named reverse() that takes two parameters: num and rev. num represents the number to be reversed, and rev represents the reversed number obtained so far.
  2. Inside the reverse() function, it checks if num is equal to 0. If it is, it means that all the digits have been processed, so it returns the rev as the final reversed number.
  3. If num is not equal to 0, the code proceeds to the else block. It performs the following steps:
    • It extracts the unit digit of num by using the modulus operator % with 10. This gives the remainder when num is divided by 10, which is the unit digit.
    • It multiplies the current rev by 10 and adds the extracted digit to it. This effectively shifts the existing digits in rev to the left and adds the new digit at the rightmost position.
    • It then recursively calls the reverse() function with num divided by 10 (to remove the unit digit) and the updated rev as arguments.
  4. The recursion continues until num becomes 0. At this point, the base case is triggered, and the reversed number (rev) is returned.
  5. In the main part of the code, it takes input from the user for the number to be reversed, using the input() function and int() function to convert the input to an integer. The entered number is stored in the variable num.
  6. The program calls the reverse() function with num as the first argument and 0 as the initial value of rev. The result is stored in a variable.
  7. Finally, the program prints the reversed number obtained by calling the reverse() function, using the print() function.