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
4321
In this program, while loop is used to reverse a number as given in the following steps:
- First, the remainder of the
num
divided by 10 is stored in the variabledigit
. Now, thedigit
contains the last digit ofnum
, 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
contains0 * 10 + 4 = 4
.num
is then divided by 10 so that now it only contains the first three digits: 123. - After second iteration,
digit
equals 3, reversed equals4 * 10 + 3 = 43
andnum = 12
. - After third iteration,
digit
equals 2, reversed equals43 * 10 + 2 = 432
andnum = 1
. - After fourth iteration,
digit
equals 1, reversed equals432 * 10 + 1 = 4321
andnum = 0
. - Now
num = 0
, so the test expressionnum != 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:
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:
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
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 |
Recommended videos for you
Application of Clustering in Data Science Using Real-Time Examples
Watch Now
Python Loops – While, For and Nested Loops in Python Programming
Watch Now
Python Numpy Tutorial – Arrays In Python
Watch Now
Business Analytics Decision Tree in R
Watch Now
Python Programming – Learn Python Programming From Scratch
Watch Now
The Whys and Hows of Predictive Modeling-II
Watch Now
Python Tutorial – All You Need To Know In Python Programming
Watch Now
Python Classes – Python Programming Tutorial
Watch Now
Android Development : Using Android 5.0 Lollipop
Watch Now
Mastering Python : An Excellent tool for Web Scraping and Data Analysis
Watch Now
3 Scenarios Where Predictive Analytics is a Must
Watch Now
Data Science : Make Smarter Business Decisions
Watch Now
Web Scraping And Analytics With Python
Watch Now
Sentiment Analysis In Retail Domain
Watch Now
Python List, Tuple, String, Set And Dictonary – Python Sequences
Watch Now
Recommended blogs for you
Google Data Science Interview Questions : All you need to know to crack It
Read Article
Lists In Python: Everything You Need To Know About Python Lists
Read Article
Top 50 Important OOPs Interview Questions and Answers in 2022
Read Article
Introduction To Python- All You Need To know About Python
Read Article
Top Deep Learning Interview Questions You Must Know in 2022
Read Article
All You Need To Know About Principal Component Analysis [PCA]
Read Article
Top Machine Learning Interview Questions You Must Prepare In 2022
Read Article
Why Python Training is Essential for Big Data Jobs?
Read Article
What Is Data Science? A Beginner’s Guide To Data Science
Read Article
How to Implement Membership Operators in Python
Read Article