How do you invert a number in python?

Example 1: Reverse a Number using a while loop

num = 1234
reversed_num = 0

while num != 0:
    digit = num % 10
    reversed_num = reversed_num * 10 + digit
    num //= 10

print("Reversed Number: " + str(reversed_num))

Output

Show
4321

In this program, while loop is used to reverse a number as given in the following steps:

  1. First, the remainder of the num divided by 10 is stored in the variable digit. Now, the digit contains the last digit of num, i.e. 4.
    digit is then added to the variable reversed after multiplying it by 10. Multiplication by 10 adds a new place in the reversed number. One-th place multiplied by 10 gives you tenth place, tenth gives you hundredth, and so on. In this case, reversed_num contains 0 * 10 + 4 = 4.
    num is then divided by 10 so that now it only contains the first three digits: 123.
  2. After second iteration, digit equals 3, reversed equals 4 * 10 + 3 = 43 and num = 12.
  3. After third iteration, digit equals 2, reversed equals 43 * 10 + 2 = 432 and num = 1.
  4. After fourth iteration, digit equals 1, reversed equals 432 * 10 + 1 = 4321 and num = 0.
  5. Now num = 0, so the test expression num != 0 fails and while loop exits. reversed already contains the reversed number 4321.

Example 2: Using String slicing

num = 123456
print(str(num)[::-1])

Output

654321

Using the string slicing concept, you can get reverse the string. ::-1 corresponds to start:stop:step. When you pass -1 as step, the start point goes to the end and stop at the front.

Published on Sep 24,2019 64.4K Views


7 / 11 Blog from Python Programs

Python is an interpreted, high-level, general-purpose programming language with different applications. To learn the fundamental concepts of Python, there are some standard programs which would give you a brief understanding of all the concepts practically. Reverse a number in Python is one of these programs which gives the learner a deep understanding of loops and arithmetic operators. This blog will help you understand and implement the ways to reverse a number. It will cover the following topics –

  • How to reverse a number in Python?
  • Python program to reverse a number
    • Using loops
    • Using recursion

How to reverse a number in Python?

It’s simple! You can write a Python program which takes input number and reverse the same. The value of an integer is stored in a variable which is checked using a condition and then each digit of the number is stored in another variable, which will print the reversed number. Numbers can be reversed in Python using different methods, let us take a look at the Python program to implement the same.

Python program to reverse a number

There are two ways to reverse a number in Python programming language –

  • Using a Loop
  • Using Recursion

Reverse a Number using Loop

 # Get the number from user manually 
num = int(input("Enter your favourite number: "))

# Initiate value to null
test_num = 0

# Check using while loop

while(num>0):
  #Logic
  remainder = num % 10
  test_num = (test_num * 10) + remainder
  num = num//10

# Display the result
print("The reverse number is : {}".format(test_num))

Output:

How do you invert a number in python?

Program Explanation

User value: Number = 123456 and Reverse = 0

First Iteration
Reminder = Number %10
Reminder = 123456%10 = 6
Reverse = Reverse *10 + Reminder
Reverse = 0 * 10 + 6 = 0 + 6 = 6
Number = Number //10
Number = 123456 //10 = 12345

Second Iteration
From the first Iteration the values of both Number and Reverse have been changed as: Number = 12345 and Reverse = 6
Reminder = Number % 10
Reminder = 12345 % 10 = 5
Reverse = Reverse *10+ Reminder = 6 * 10 + 5
Reverse = 60 + 5 = 65
Number = Number //10 = 12345 //10
Number = 1234

Third Iteration
From the Second Iteration, the values of both Number and Reverse have been changed as: Number = 1234 and Reverse = 65
Reminder = Number %10
Reminder = 1234%10 = 4
Reverse = Reverse *10+ Reminder = 65 * 10 + 4
Reverse = 650 + 4 = 654
Number = Number //10 = 1234//10
Number = 123

Fourth Iteration
From the Second Iteration the values of both Number and Reverse have been changed as: Number = 123 and Reverse = 654
Reminder = Number %10
Reminder = 123 %10 = 3
Reverse = Reverse *10+ Reminder = 654 * 10 + 3
Reverse = 6540 + 3 = 6543
Number = Number //10 = 123//10
Number = 12

Fifth iteration
From the Second Iteration the values of both Number and Reverse have been changed as: Number = 12 and Reverse = 6543
Reminder = Number %10
Reminder = 12 %10 = 2
Reverse = Reverse *10+ Reminder = 6543 * 10 + 2
Reverse = 65430 + 2 = 65432
Number = Number //10 = 12//10
Number = 1

Sixth iteration
From the Second Iteration, the values of both Number and Reverse have been changed as, Number = 1 and Reverse = 65432
Reminder = Number %10
Reminder = 1 %10 = 1
Reverse = Reverse *10+ Reminder = 65432 * 10 + 1
Reverse = 654320 + 1 = 654321
Number ended:

Reverse a Number using Recursion

 
# Python Program to Reverse a Number using Recursion

Num = int(input("Please Enter any Number: "))

Result = 0
def Result_Int(Num):
    global Result
    if(Num > 0):
        Reminder = Num %10
        Result = (Result *10) + Reminder
        Result_Int(Num //10)
    return Result

Result = Result_Int(Num)
print("n Reverse of entered number is = %d" %Result)

Output:

How do you invert a number in python?

With this, we come to an end of this blog on “Reverse a Number in Python”. I hope it added value to your knowledge of Python programming.

To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access. Got a question for us? Mention them in the comments section of “Reverse a Number in Python” and we will get back to you.

Upcoming Batches For Python Certification Training Course

Course NameDate
Python Certification Training Course

Class Starts on 15th October,2022

15th October

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 12th November,2022

12th November

SAT&SUN (Weekend Batch)
View Details

How do you invert a number in python?

Application of Clustering in Data Science Using Real-Time Examples

Watch Now

How do you invert a number in python?

Python Loops – While, For and Nested Loops in Python Programming

Watch Now

How do you invert a number in python?

Python Numpy Tutorial – Arrays In Python

Watch Now

How do you invert a number in python?

Business Analytics Decision Tree in R

Watch Now

How do you invert a number in python?

Python Programming – Learn Python Programming From Scratch

Watch Now

How do you invert a number in python?

The Whys and Hows of Predictive Modeling-II

Watch Now

How do you invert a number in python?

Python Tutorial – All You Need To Know In Python Programming

Watch Now

How do you invert a number in python?

Python Classes – Python Programming Tutorial

Watch Now

How do you invert a number in python?

Android Development : Using Android 5.0 Lollipop

Watch Now

How do you invert a number in python?

Mastering Python : An Excellent tool for Web Scraping and Data Analysis

Watch Now

How do you invert a number in python?

3 Scenarios Where Predictive Analytics is a Must

Watch Now

How do you invert a number in python?

Data Science : Make Smarter Business Decisions

Watch Now

How do you invert a number in python?

Web Scraping And Analytics With Python

Watch Now

How do you invert a number in python?

Sentiment Analysis In Retail Domain

Watch Now

How do you invert a number in python?

Python List, Tuple, String, Set And Dictonary – Python Sequences

Watch Now

How do you invert a number in python?

Google Data Science Interview Questions : All you need to know to crack It

Read Article

How do you invert a number in python?

Lists In Python: Everything You Need To Know About Python Lists

Read Article

How do you invert a number in python?

Top 50 Important OOPs Interview Questions and Answers in 2022

Read Article

How do you invert a number in python?

Introduction To Python- All You Need To know About Python

Read Article

How do you invert a number in python?

Top Deep Learning Interview Questions You Must Know in 2022

Read Article

How do you invert a number in python?

All You Need To Know About Principal Component Analysis (PCA)

Read Article

How do you invert a number in python?

Top Machine Learning Interview Questions You Must Prepare In 2022

Read Article

How do you invert a number in python?

Why Python Training is Essential for Big Data Jobs?

Read Article

How do you invert a number in python?

What Is Data Science? A Beginner’s Guide To Data Science

Read Article

How do you invert a number in python?

How to Implement Membership Operators in Python

Read Article

How do you flip a number in Python?

Reversing a number using reversed Method We use the python reversed() function to reverse a number by this method. This function returns a pointer that can iterate the string/list in reverse order. We use the join() method of python to obtain the reversed number from this iterator finally.

How do you print reverse digits in Python?

Reverse Number In Python.
# Python Program to Reverse a Number using While loop..
Number = int(input("Please Enter any Number: ")).
Reverse = 0..
while(Number > 0):.
Reminder = Number %10..
Reverse = (Reverse *10) + Reminder..
Number = Number //10..
print("\n Reverse of entered number is = %d" %Reverse).

How do you reverse a number in Python without loops?

Reverse a Number Without using Loop in Python We read a number and reverse a number using slice operations. We will convert the integer number to string using str() and then, calculate the reverse of a number using the slicing operation. Finally, the result will be displayed on the screen.

How do you reverse input a number?

Let's see a simple c example to reverse a given number..
#include.
int main().
int n, reverse=0, rem;.
printf("Enter a number: ");.
scanf("%d", &n);.
while(n!=0).