Python Program to Reverse a Number

This article discloses the Python Program to Reverse a Number using Python While Loop, Functions, and Recursion.

Python Program to Reverse a Number Using While Loop

This python program to reverse a number allows the user to enter any positive integer. Then, this Python program reverses a number using Python While Loop

# Python Program to Reverse a Number using While loop

Number = int(input("Please Enter any Number: "))
Reverse = 0
while(Number > 0):
    Reminder = Number %10
    Reverse = (Reverse *10) + Reminder
    Number = Number //10

print("\n Reverse of entered number is = %d" %Reverse)

Python reverse a number output

Please Enter any Number: 1456

 Reverse of entered number is = 6541

This program to reverse a number in python allows the user to enter any positive integer. Then that number is assigned to variable Number.

Next, Condition in the While loop makes sure that the given number is greater than 0.

From the above Python example, User Entered value: Number = 1456 and Reverse = 0

First Iteration

Reminder = Number %10
Reminder = 1456%10 = 6

Reverse = Reverse *10 + Reminder
Reverse = 0 * 10 + 6  = 0 + 6 = 6

Number = Number //10
Number = 1456 //10 = 145

Second Iteration
From the Python While Loop first Iteration, the values of both the Number and Reverse changed as Number = 145 and Reverse = 6

Reminder = Number % 10
Reminder = 145 % 10 = 5

Reverse = Reverse *10+ Reminder = 6 * 10 + 5
Reverse = 60 + 5 = 65

Number = Number //10 = 145 //10
Number = 14

Third Iteration
From the Second Iteration of Python reverse a Number program, Number = 14 and Reverse = 65

Reminder = Number %10
Reminder = 14%10 = 4

Reverse = Reverse *10+ Reminder = 65 * 10 + 4
Reverse = 650 + 4 = 654

Number = Number //10 = 14//10
Number = 1

Fourth Iteration
From the third Iteration, Number = 1 and Reverse = 654

Reminder = Number %10
Reminder = 1 %10 = 1

Reverse = Reverse *10+ Reminder = 654 * 10 + 1
Reverse = 6540 + 1 = 6541

Number = Number //10 = 1//10
Number = 0

Here, For the next iteration, Number = 0. So, the while loop condition fails.

Program to Reverse a Number Using Functions

This program to reverse a number in python allows the user to enter any positive integer. Then we are going to reverse a number using the reverse Functions.

# Python Program to Reverse a Number using Functions

def Reverse_Integer(Number):
    Reverse = 0
    while(Number > 0):
        Reminder = Number %10
        Reverse = (Reverse *10) + Reminder
        Number = Number //10
    return Reverse

Number = int(input("Please Enter any Number: "))
Reverse = Reverse_Integer(Number)
print("\n Reverse of entered number is = %d" %Reverse)
Please Enter any Number: 234589

 Reverse of entered number is = 985432

Within this reverse number in a program, When it reaches to Reverse = Reverse_Integer (Number) line in the program then the compiler immediately jump to below function:

def Reverse_Integer(Number):

The last line ends with a return Reverse statement.

Python Program to Reverse a Number using Recursion

This program to reverse a number allows the user to enter any positive integer. And then, we are going to reverse a number using Python Recursion

# Python Program to Reverse a Number using Recursion

Reverse = 0
def Reverse_Integer(Number):
    global Reverse
    if(Number > 0):
        Reminder = Number %10
        Reverse = (Reverse *10) + Reminder
        Reverse_Integer(Number //10)
    return Reverse

Number = int(input("Please Enter any Number: "))
Reverse = Reverse_Integer(Number)
print("\n Reverse of entered number is = %d" %Reverse)
Python Program to Reverse a Number Using Recursion

In this reverse a number program, When the compiler reaches to Reverse = Reverse_Integer (Number) line in the program then the compiler immediately jump to below function:

def Reverse_Integer(Number):

In this reverse number function, the below statement helps to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it terminates.

Reverse_Integer(Number //10)

For example, Number = 459 produces the output as 9

Let’s see the Python If Statement,

if (Number > 0), check whether the number is greater than 0 or not. For Recursive functions, it is essential to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like infinite Loop).

Comments are closed.