In this tutorial we are going to learn Writing a python program to reverse the digits of a given number in Python programming language. Reversing a number means that the digits in the number are flipped in order. For example, the number 12345 will becomes 54321 after reverse. There are several ways to reverse a number in python programing. We can use for loops, while loops, slicing, or string manipulation to reverse the number. Below we have explored all these different methods in detail.
Problem Statement:
Here we have to write a Python program that will takes an integer as input from the user. After taking number from the user it will reverses the order of its digits. The program should then output the reversed number.
For Example
Enter a number: 12345
Reverse of the number: 54321
In the above example user has given 12345 as input number. The program reverses the order of the digits to obtain 54321, which is output as the reverse number.
Our basic idea behind solving this program to extract the digits of the user input one by one. Append the extracted digit to a new number each time to get the reverse order.
Method 1: Reverse Using While Loop in Python
n = int(input("Please give a number: "))
print("Before reverse your number is : %d" %n)
reverse = 0
while n!=0:
reverse = reverse*10 + n%10
n = (n//10)
print("After reverse : %d" %reverse)
Output:
please give a number : 12345
before reverse your number is : 12345
After reverse : 54321
Explanation:
- In the above program we have taken the input number from the user using the
input()
function. - With the help of
int()
function we have converted input to an integer. - Initialized a variable
reverse
to 0 to assign the reversed number. - We have used a while loop to reverse the number.
- In each iteration we have taken the last digit of the input number using the modulo operator
%
. - After that we are adding this digit to
reverse
variable by multiplied by 10 in<code>reverse
variable. This adds the digit to the leftmost position in<code><code>reverse
. - Remove the last digit from the input number using integer division
//
, which discards any remainder. - This process continues until all the digits of the input number have been reversed.
- At last, print the reversed number using the
print()
function of python.
Method 2: Reverse Using String Slicing in Python
num = int(input("Please give a number: "))
print("Before reverse your number is : %d" %num)
rev = int(str(num)[::-1])
print("After reverse the number:", rev)
Output:
Please give a number: 12345
Before reverse your number is : 12345
After reverse the number: 54321
Explanation:
- First taken the input number from the user with the help of input() function of python.
- Then Convert it to an integer using the int() function.
- After that we have Converted the integer to a string using the str() function.
- We have used slicing to reverse the string.
- In Python, Slicing operation
[::-1]
creates a new string that is a reversed copy of the original string. - Converted the reversed string back in the integer using
int()
function of python. - Finally, print the reversed number using the print() function.
Method 3: Reverse Using Recursion in Python
Another way to reverse a number in Python is to use recursion.
def reverse(num):
if num < 10:
return num
else:
return (num % 10) * 10**(len(str(num))-1) + reverse(num // 10)
num = int(input("Please enter a number: "))
print("Before reverse your number is : %d" %num)
rev = reverse(num)
print("After reverse the number:", rev)
Output
Please give a number: 12345
Before reverse your number is : 12345
After reverse the number: 54321
Explanation:
- With the help of input function we are taking integer input from user.
- Converted input value into integer using int() function.
- We created a recursive function reverse() function that takes an integer
num
as input and returns reversed value. - Base case of the recursion is when
num
is less than 10 simply returnnum
as it is. - In the recursive case, we take the last digit of
num
using the modulo operator%
and multiply it by 10 raised to the power of the number of digits innum
minus one. We then add this value to the result of the recursive call to reverse() onnum
divided by 10 using integer division. - Finally print the output returned by reverse() function that is stored in rev variable.
Conclusion:
In this post, we have explained three different ways to reverse a given number using Python Programming. Each method has its own advantages and disadvantages, and the choice of method depends on the requirements. The first method using a while loop is generally the most efficient and straightforward, while the second method using string slicing is the most concise and readable. The third method using recursion is an interesting alternative that may be useful in certain situations.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…