How to Reverse an Integer in Python

Python is used to perform various types of tasks. Python has many built-in standard data types like integer, complex number, string, and floating-point number. The arithmetic operations can be performed on integers, complex, and floating-point numbers. Many operations can be performed on numbers.

Reversing an integer in Python is also a common operation that gives the deep knowledge of Python loops and recursive functions to Python developers.

For example, if we have an integer 12345, the reverse form of the integer is 54321.

This tutorial demonstrates the procedures to reverse an integer in Python.

Reverse an integer in Python

In Python, an integer can be revered in the two following ways:

  1. Reverse an integer using the loop
  2. Reverse an integer using recursive function

Method 1: Reverse an integer using the loop

We can easily implement a loop program in Python, which takes the input from the user and store it in a variable. Each digit on an integer is stored in a new temporary variable and at the end, we get the reverse integer.

Let’s implement a program using loop to reverse an integer.

#getting the integer number of user as input
#converting the user input into int 
num = int(input("Enter an integer: "))
#declaring a temp variable
#temp variable is initilized to zero
temp_num = 0 
#implementing a while loop 
while(num>0):
  #implementing the logic to reverse an integer
  remainder = num % 10
  temp_num = (temp_num * 10) + remainder
  num = num//10
print("The reverse integer is : {}".format(temp_num ))

Enter an integer : 987

The reverse integer is : 789

Also Learn: How to find List Length in Python

Explanation of program

A user enters a value that is converted into an integer and stored in a num variable. Inside the while loop, the number is divided by 10.

Following iterations will be performed if we enter number 987.

First iteration

In the first iteration, the integer value will be 987.

Remainder = 987%10 = 7
temp_num= temp_num*10 + Reminder 
temp_number= 0*10+7=7
num=987//10=98

Second iteration

In the second iteration, now the number value is changed to 98 and temp_num value is 7.

Remainder = 98%10 = 8
temp_num= temp_num*10 + Reminder 
temp_number= 7*10+8=78
num=98//10=9

Third iteration

In the third and final iteration, the number value is 9 and temp value is 78.
Remainder = 9%10 = 9
temp_num= temp_num*10 + Reminder 
temp_number= 78*10+9=789
Now the reverse value is 789 and num ended.

Method 2: Reverse an integer using recursive function

An integer can be reversed using the recursive function. In recursive function, a problem is divided into smaller parts and then it is solved. Following the piece of code for reversing an integer using recursive function:

num = int(input("Enter an integer: "))
result = 0
def reverse_int(num):
    global result
    if(num > 0):
        Reminder = num %10
        result = (result *10) + Reminder
        reverse_int(num //10)
    return result
result = reverse_int(num)
print("The reverse integer is:",result)

Conclusion

Python supports many built-in data types like int, float, string and etc. An integer can be revered in Python using loop and recursive function. This guide demonstrates the procedures to reverse an integer in Python.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Subscribe to get IQ's , Tutorials & Courses