Reversing an integer number is an easy task. We may encounter certain scenarios where it will be required to reverse a number.
Input: 12345 Output: 54321There are two ways, we can reverse a number −
Convert the number into a string, reverse the string and reconvert it into an integer
Reverse mathematically without converting into a string
Convert into string and Reverse
This method of reversing a number is easy and doesn’t require any logic. We will simply convert the number into string and reverse it and then reconvert the reversed string into integer. We can use any suitable method for reversing the string.
Example
Live Demo
def reverse(num): st=str(num) revst=st[::-1] ans=int(revst) return ans num=12345 print(reverse(num))Output
54321Reverse mathematically without converting into string
This method requires mathematical logic. This method can be used when there is a restriction of not converting the number into string.
Example
Live Demo
Output
54321
Updated on 10-Mar-2021 14:12:28
- Related Questions & Answers
- How to reverse a string in Python?
- Reverse a number in JavaScript
- How to create reverse of a number in R?
- C++ Program to Reverse a Number
- Java Program to Reverse a Number
- Python program to reverse bits of a positive integer number?
- How to reverse a string in Python program?
- Reverse a Number in PL/SQL
- Write a Golang program to reverse a number
- Reverse a number using stack in C++
- How to reverse the objects in a list in Python?
- Write a program to reverse digits of a number in C++
- Program to reverse a linked list in Python
- Python program to Reverse a range in list
- Python program to reverse a Numpy array?
This is a Python Program to reverse a given number. Problem Description The program takes a number and reverses it. Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and store the reversed number in another variable.
3. Print the reverse of the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to reverse a given number.
n=int(input("Enter number: ")) rev=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 print("Reverse of the number:",rev)
Program Explanation
1. User must first enter the value and store it in a variable n.
2. The while loop is used and the last digit of the number is obtained by using the modulus operator.
3. The last digit is then stored at the one’s place, second last at the ten’s place and so on.
4. The last digit is then removed by truly dividing the number
with 10.
5. This loop terminates when the value of the number is 0.
6. The reverse of the number is then printed.
Runtime Test Cases
Case 1: Enter number: 124 Reverse of the number: 421 Case 2: Enter number: 4538 Reverse of the number: 8354
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.