Python Program to find the Reverse of a given number​

Reverse a number in python programming

Find the Reverse of a Number in Python

Given an integer input the objective is to reverse the given integer number using loops and slicing. Therefore, we’ll write a program to Find the Reverse of a Number in Python Language.

Example
Input : 123
Output : 321

Find the Reverse of a Number in Python Language

Given an integer input as the number, the objective is to find the reverse of the given number input using loops and slicing methods in Python. The typical method is to use modulo and divide operators to break down the number and reassemble again in the reverse order. Here are some of the methods to solve the above mentioned problems,

  • Method 1: Using Simple Iteration
  • Method 2: Using String Slicing
  • Method 3: Using Recursion

Let’s go through the above mentioned methods in the sections below.

Reversing a Number using Recursion in C

Method 1: Using Simple Iteration

Working

In this method we’ll use the concept of loops to repeat the process of breaking down the number and adding it back in the reverse order.

For a given integer variable number we perform the following,

  • Run a while loop with the condition number >0.
    • Extract the last digit as Remainder using Modulo operator.
    • Using the formula reverse = ( reverse * 10 ) + remainder , we keep changing the reverse value.
    • Break down the Nunber using divide operator.
  • Print the reverse variable.

Let’s implement the above mentioned Logic in Python Language.

Python Code

Run
num = 1234
temp = num
reverse = 0
while num > 0:
    remainder = num % 10
    reverse = (reverse * 10) + remainder
    num = num // 10

print(reverse)

Output

4321

Method 2: Using String Slicing

Working

In this method we’ll use the string slicing methods to reverse the number. First we’ll convert the number into a string and then using the string slicing property in python language, we’ll slice the string to break it down and add it up in reverse order.

For a given integer input, we perform the following operations,

  • Convert the number into string format.
  • Reverse the number using string slicing.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
num = 1234
print(str(num)[::-1])

Output

4321

Method 3: Using Recursion

Working

In this method we’ll use recursion to perform all the operations we perform in method 1. We’ll use the same formula but instead of iterating through a loop, we’ll use recursion. To know more about recursion, check out our page Recursion in Python.

Given an integer input number, we perform the following,

  • Define a recursive function recursum() that takes in number and reverse variable as arguments.
  • Set the base case as number == 0 and step recursive call as recursum(num/10, reverse).
  • Print the returned value using print() function.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
def recursum(number,reverse):
  if number==0:
    return reverse
  remainder = int(number%10)
  reverse = (reverse*10)+remainder
  return recursum(int(number/10),reverse)

num = 1234
reverse = 0
print(recursum(num,reverse))

Output

4321

32 comments on “Python Program to find the Reverse of a given number​”


  • Altada

    is this correct approach?
    num=input(“enter the number:”)
    i=num[::-1]
    print(int(i))


  • Varsha

    def getReverse(num):
    res=0
    while num:
    remainder = num%10
    num = num//10
    res = res*10+remainder
    return res
    print(getReverse(int(input())))