Python program to reverse a number with explanation

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 return num 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 in num minus one. We then add this value to the result of the recursive call to reverse() on num 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.

Leave a Comment