Reverse a Number in Python using While Loop

We will develop a program to reverse a number in python using while loop. To reverse a number we need to extract the last digit of the number and add that number into a temporary variable with some calculation, then remove the last digit of the number. Do these processes until the number becomes zero.

To remove the last digit the modulus operator (%) will be helpful for us, and to remove the last digit division operator (/) can be used. To add the last digit first we need to multiply the previous value by 10. Once you get the logic, you can write the program in any language.

Example of the reverse a number:-
Number: 56021
Reverse number: 12065

Procedure to find reverse of a number,

  1. Take a number
  2. Declare a temporary variable and initialize it with 0
  3. Find the last digit of the number
  4. Multiply the temporary variable by 10
  5. Add that last digit to the temporary variable
  6. Remove the last digit of the number.
  7. Repeat this process from 3 to 6 until the number becomes 0.

Reverse a Number using While Loop in Python

We will take integer numbers while declaring the variables. Then, find the reverse of a number using the while loop and finally, the result will be displayed on the screen.

# Python program to reverse a number using while loop

# take inputs
num = 12345

# calculate reverse of number
reverse = 0
while(num > 0):
    last_digit = num % 10
    reverse = reverse * 10 + last_digit
    num = num // 10

# display result
print('The reverse number is =', reverse)

Output:-

The reverse number is = 54321

Reverse a Number in Python using While Loop

In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.

# Python program to reverse a number using while loop

# take inputs
num = int(input('Enter a number: '))

# calculate reverse of number
reverse = 0
while(num > 0):
    last_digit = num % 10
    reverse = reverse * 10 + last_digit
    num = num // 10

# display result
print('The reverse number is =', reverse)

Output for the input values test-case-1:-

Enter a number: 416064
The reverse number is = 460614

Output for the input values test-case-2:-

Enter a number: 164532
The reverse number is = 235461

Python Program to Reverse a Number using While Loop

We can also take the help of a function to reverse a number in python using while loop. A function is a block of code that performs a specific task.

# Python program to reverse a number using while loop

def findReverse(n):  #user-defined function
   # calculate reverse of number
   reverse = 0
   reminder = 0
   while(n != 0):
      remainder = n % 10
      reverse = reverse * 10 + remainder
      n = int(n / 10)
   return reverse

# take inputs
num = int(input('Enter the number: '))

# calling function and display result
reverse = findReverse(num)
print('The reverse number is =', reverse)

Output:-

Enter the number: 46894
The reverse number is = 49864

Leave a Comment

Your email address will not be published. Required fields are marked *