Reverse a Number in Python

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

We need to write a python code to reverse the given integer and print the integer. 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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

43 comments on “Reverse a Number in Python”


  • Ankit

    simplest reverse of a number and string also
    num = input(“enter the number: “)
    sum = num[::-1]
    print(sum)


  • Anirudh

    num =str(input())
    print(“The number is “,num)
    print (“The Reverse number is :”,num[::-1])


  • Hrishikesh

    n=int(input(“Enter any num\n”))
    print(“{} is reversed to {}”.format(str(n),str(n)[::-1]))


  • 44_Harsh

    n = str(input(“enter number to reverse”))
    a= n[::-1]
    b = int(a)
    print(“reversed number is =>”,b)


  • Uzaif

    #Reverse a Number :
    n=int(input())
    n=list(map(int,str(n)))
    print(*n[::-1],sep=””)


  • Mda

    n=int(input(“Enter number “))
    new=str(n)
    rev=new[::-1]
    print(int(rev))


  • Mda

    def ReverseNum(num):
    revNum = 0
    temp=num
    while(num > 0):
    rem = num % 10
    revNum = (revNum*10) + rem
    num = num//10
    print(f”{temp} is reversed to {revNum}”)
    ReverseNum(int(input(“Enter the number to reverse”)))


  • Aditya

    # reverse of number program:-
    def reverse_num(num):
    new_num=str(num)
    return int(new_num[::-1])
    print(reverse_num(45678)) # 87654


  • asatiayush0

    number = [int(d) for d in input(“Enter number: “)]
    print(“”.join(str(d) for d in reversed(number)))