Reverse a Number in Python

Wajiha Urooj
Edureka
Published in
4 min readSep 24, 2019

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 that 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 article 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 that takes an input number and reverse the same. The value of an integer is stored in a variable that 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

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 RecursionNum = 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:

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Python Modules

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21. Python Pattern Programs

22. Web Scraping With Python

23. Python Decorator

24. Python Spyder IDE

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

Originally published at https://www.edureka.co on September 24, 2019.

--

--