# How to reverse a number in Python?

It is the most asked programming question in the interview. We can reverse the integer number in Python using the different methods.

Here we will write the program which takes input number and reversed the same. Let’s understand the following methods of reversing the integer number.

• Using while loop
• Using recursion

### Reverse a number using Python while loop

First, we understand the algorithm of this program. It will make easy to understand the program logic. Once you get the logic, you can write the program in any language, not only Python.

### Algorithm

Input Integer: number
(1) Initialize variable revs_number = 0
(2) Loop while number > 0
(a) Multiply revs_number by 10 and add remainder of number
divide by 10 to revs_number
revs_number = revs_number*10 + number%10;
(b) Divide num by 10
(3) Return revs_number

Let’s implement the above algorithm in program.

# Ask for enter the number from the use

number = int(input("Enter the integer number: "))

revs_number = 0

# reverse the integer number using the while loop

while (number > 0):
# Logic
remainder = number % 10
revs_number = (revs_number * 10) + remainder
number = number // 10

# Display the result

print(“The reverse number is : {}”.format(revs_number))

Output:

``````Enter the integer number: 12345
The reverse number is: 54321
``````

Explanation -

Let’s understand this program step by step.

We initialed a number variable for user input and variable revs_number initial value to null.

First Iteration

``````Reminder = number %10
Reminder = 12345%10 = 5
Reverse = Reverse *10 + Reminder Initial value of revs_number is null
Reverse = 0 * 10 + 5 = 0 + 5 = 5
Number = Number //10
Number = 1234 //10 = 1234 // Now loop will iterate on this number.
``````

Second Iteration

Now the number is 123, and the revs_number is 5. The while checks its condition and executes for the next iteration.

``````Reminder = Number % 10
Reminder = 1234 % 10 = 4
Reverse = Reverse *10+ Reminder = 5 * 10 + 4
Reverse = 50 + 4 = 54
Number = Number //10 = 12345 //10
Number = 123
``````

Third Iteration

From the Second Iteration, the values of both Number and Reverse have been changed as: number = 123 and revs_number = 54

``````Reminder = Number %10
Reminder = 123%10 = 3
Reverse = Reverse *10+ Reminder = 54 * 10 + 3
Reverse = 540 + 3 = 543
Number = Number //10 = 123//10
Number = 12
``````

# How to reverse a number in Python

It is the most asked programming question in the interview. We can reverse the integer number in Python using the different methods.

Here we will write the program which takes input number and reversed the same. Let’s understand the following methods of reversing the integer number.

• Using while loop
• Using recursion

### Reverse a number using Python while loop

First, we understand the algorithm of this program. It will make easy to understand the program logic. Once you get the logic, you can write the program in any language, not only Python.

### Algorithm

1. Input Integer: number
2. (1) Initialize variable revs_number = 0
3. (2) Loop while number > 0
4. (a) Multiply revs_number by 10 and add remainder of number
5. divide by 10 to revs_number
6. revs_number = revs_number*10 + number%10;
7. (b) Divide num by 10
8. (3) Return revs_number

Let’s implement the above algorithm in program.

1. # Ask for enter the number from the use

2. number = int(input("Enter the integer number: "))
• # Initiate value to null

1. revs_number = 0
• # reverse the integer number using the while loop

• while (number > 0):
1. # Logic

2. remainder = number % 10
3. revs_number = (revs_number * 10) + remainder
4. number = number // 10
• # Display the result

1. print(“The reverse number is : {}”.format(revs_number))

Output:

Enter the integer number: 12345 The reverse number is: 54321

Explanation -

Let’s understand this program step by step.

We initialed a number variable for user input and variable revs_number initial value to null.

First Iteration

Reminder = number %10
Reminder = 12345%10 = 5
Reverse = Reverse *10 + Reminder Initial value of revs_number is null
Reverse = 0 * 10 + 5 = 0 + 5 = 5
Number = Number //10
Number = 1234 //10 = 1234 // Now loop will iterate on this number.

Second Iteration

Now the number is 123, and the revs_number is 5. The while checks its condition and executes for the next iteration.

Reminder = Number % 10
Reminder = 1234 % 10 = 4
Reverse = Reverse *10+ Reminder = 5 * 10 + 4
Reverse = 50 + 4 = 54
Number = Number //10 = 12345 //10
Number = 123

Third Iteration

From the Second Iteration, the values of both Number and Reverse have been changed as: number = 123 and revs_number = 54

Reminder = Number %10
Reminder = 123%10 = 3
Reverse = Reverse *10+ Reminder = 54 * 10 + 3
Reverse = 540 + 3 = 543
Number = Number //10 = 123//10
Number = 12

Fourth Iteration

The modified number is 12 and the revs_number is 543: Now while executes again.

``````Reminder = Number %10
Reminder = 12 %10 = 2
Reverse = Reverse *10+ Reminder = 543 * 10 + 2
Reverse = 5430 + 2 = 5432
Number = Number //10 = 12//10
Number = 1
``````

Fifth Iteration

``````Reminder = Number %10
Reminder = 1 %1 0 = 1
Reverse = Reverse *10+ Reminder = 5432 * 10 + 1
Reverse = 54320 + 1 = 54321
``````

while loop is terminated because if found the false as a Boolean result.

You can enter the different number and check the result.

### Reverse a Number Using Recursion

Let’s understand the following example.

``````num = int(input("Enter the number: "))
revr_num = 0    # initial value is 0. It will hold the reversed number
def recur_reverse(num):
global revr_num   # We can use it out of the function
if (num > 0):
Reminder = num % 10
revr_num = (revr_num * 10) + Reminder
recur_reverse(num // 10)
return revr_num

revr_num = recur_reverse(num)
print("n Reverse of entered number is = %d" % revr_num)
``````

# How to reverse a number in Python

It is the most asked programming question in the interview. We can reverse the integer number in Python using the different methods.

Here we will write the program which takes input number and reversed the same. Let’s understand the following methods of reversing the integer number.

• Using while loop
• Using recursion

### Reverse a number using Python while loop

First, we understand the algorithm of this program. It will make easy to understand the program logic. Once you get the logic, you can write the program in any language, not only Python.

### Algorithm

1. Input Integer: number
2. (1) Initialize variable revs_number = 0
3. (2) Loop while number > 0
4. (a) Multiply revs_number by 10 and add remainder of number
5. divide by 10 to revs_number
6. revs_number = revs_number*10 + number%10;
7. (b) Divide num by 10
8. (3) Return revs_number

Let’s implement the above algorithm in program.

1. # Ask for enter the number from the use

2. number = int(input("Enter the integer number: "))
• # Initiate value to null

1. revs_number = 0
• # reverse the integer number using the while loop

• while (number > 0):
1. # Logic

2. remainder = number % 10
3. revs_number = (revs_number * 10) + remainder
4. number = number // 10
• # Display the result

1. print(“The reverse number is : {}”.format(revs_number))

Output:

Enter the integer number: 12345 The reverse number is: 54321

Explanation -

Let’s understand this program step by step.

We initialed a number variable for user input and variable revs_number initial value to null.

First Iteration

Reminder = number %10
Reminder = 12345%10 = 5
Reverse = Reverse *10 + Reminder Initial value of revs_number is null
Reverse = 0 * 10 + 5 = 0 + 5 = 5
Number = Number //10
Number = 1234 //10 = 1234 // Now loop will iterate on this number.

Second Iteration

Now the number is 123, and the revs_number is 5. The while checks its condition and executes for the next iteration.

Reminder = Number % 10
Reminder = 1234 % 10 = 4
Reverse = Reverse *10+ Reminder = 5 * 10 + 4
Reverse = 50 + 4 = 54
Number = Number //10 = 12345 //10
Number = 123

Third Iteration

From the Second Iteration, the values of both Number and Reverse have been changed as: number = 123 and revs_number = 54

Reminder = Number %10
Reminder = 123%10 = 3
Reverse = Reverse *10+ Reminder = 54 * 10 + 3
Reverse = 540 + 3 = 543
Number = Number //10 = 123//10
Number = 12

Fourth Iteration

The modified number is 12 and the revs_number is 543: Now while executes again.

Reminder = Number %10
Reminder = 12 %10 = 2
Reverse = Reverse *10+ Reminder = 543 * 10 + 2
Reverse = 5430 + 2 = 5432
Number = Number //10 = 12//10
Number = 1

Fifth Iteration

Reminder = Number %10
Reminder = 1 %1 0 = 1
Reverse = Reverse *10+ Reminder = 5432 * 10 + 1
Reverse = 54320 + 1 = 54321

while loop is terminated because if found the false as a Boolean result.

You can enter the different number and check the result.

### Reverse a Number Using Recursion

Let’s understand the following example.

``````num = int(input("Enter the number: "))
revr_num = 0    # initial value is 0. It will hold the reversed number
def recur_reverse(num):
global revr_num   # We can use it out of the function
if (num > 0):
Reminder = num % 10
revr_num = (revr_num * 10) + Reminder
recur_reverse(num // 10)
return revr_num

revr_num = recur_reverse(num)
print("n Reverse of entered number is = %d" % revr_num)
``````

Output:

``````Enter the number: 5426
The Reverse of entered number is = 6245
``````

Logic is same in both programs. Once you understand the logic, it will easy to do it by own.