# Reverse a Number in Python using For Loop

We will develop a program to reverse a number in python using for 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.

Example of reverse number:-
Number: 49813
Reverse Number: 31894

## Reverse a Number using For Loop in Python

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

Program Description:- Write a program to reverse a number in Python

``````# Python program to reverse a number using for loop

# take inputs
num = '105'

# calculate reverse of number
reverse = ''
for i in range(len(num), 0, -1):
reverse += num[i-1]

# print reverse of number
print('The reverse number is =', reverse)
``````

Output:-

The reverse number is = 501

## Reverse a Number in Python using For 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 for loop

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

# calculate reverse of number
reverse = ''
for i in range(len(num), 0, -1):
reverse += num[i-1]

# print reverse of number
print('The reverse number is =', reverse)
``````

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

Enter the number: 984061
The reverse number is = 160489

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

Enter the number: 49815
The reverse number is = 51894

## Python Program to Reverse a Number using For Loop

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

``````# Python program to reverse a number using for loop

def findReverse(n):  #user-defined function
# calculate reverse of number
reverse = ''
for i in range(len(n), 0, -1):
reverse += n[i-1]
return reverse

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

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

Output:-

Enter the number: 765496
The reverse number is = 694567

## Program to Reverse a Number using For Loop in Python

We can also take the help of a built-in function to reverse a number in python using for loop. Python reversed() method returns an iterator that accesses the given sequence in the reverse order.

``````# Python program to reverse a number using for loop

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

# calculate reverse of number
print('The reverse number is:')
for i in reversed(num):
print(i, end='')``````

Output:-

Enter a number: 980456
The reverse number is:
654089