reverse a number

Write a Program to Reverse a Number in Python | Reverse Digits or Integers

Given a number, thee task is to reverse the given number in Python.

Examples:

Example1:

Input:

number=12345

Output:

The reversed number = 54321

Explanation:

After reversing the number we get 54321

Example2:

Input:

number=7341

Output:

The reversed number = 1437

Example3:

Input:

number=9840

Output:

The reversed number = 489

Explanation:

Here the reversed number is 0489 we neglect the leading zero so the reversed number is 489

Reverse the given Number in Python

There are several ways to reverse the given number in python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using while loop

Algorithm:

  • Scan the given number
  • Set the variable reverse_number to 0.
  • Loop while number > 0 Loop while number > 0
  • Multiply reverse_number by 10 and add the remainder to reverse_number like below
  • reverse_number = (reverse_number * 10) + remainder
  • Divide the given number by 10 to remove the last digit.
  • Print the reversed number

Below is the implemenatation:

# given number
given_num = 12345

# Take a variable reverse_number and initialize it to null
reverse_number = 0

# using while loop to reverse the given number

while (given_num > 0):
    # implementing the algorithm
    # getting the last digit
    remainder = given_num % 10
    reverse_number = (reverse_number * 10) + remainder
    given_num = given_num // 10

# Display the result
print("The reversed number =", reverse_number)

Output:

The reversed number = 54321

Method #2: Using for loop and string concatenation

Approach: 

  • Scan the given number.
  • Take a empty string say revstring.
  • Convert the given number to string using str() function.
  • Traverse every character of the string using for loop in reverse order using range function.
  • Add each character to revstring using string concatenation.
  • Print the revstring.

Below is the implementation:

1)For numbers without trailing zeroes

# given number
given_num = 12345
# taking empty string
reverse_string = ""
# Convert the given_num to string using str
strnum = str(given_num)
# calculating the length of string
length = len(strnum)
# Traverse the strnum string in reverse order using for loop range function
for index in range(length-1, -1, -1):
    # add the character to reverse_string using string concatenation
    reverse_string = reverse_string+strnum[index]
# print the result
print("The reversed number =", reverse_string)

Output:

The reversed number = 54321

Note:

Here it gives the correct result as their are no trailing zeroes

Let us consider a case where the given number contains trailing zeroes .

EX: 9840

The above algorithm gives the output

The reversed number = 0489

Here it also prints the leading zeroes so to avoid this the solution is given below.

Solution:

After getting the reversed string convert the string to integer using int() function which removes the leading zeroes

as below.

2)For numbers with trailing zeroes

# given number
given_num = 9840
# taking empty string
reverse_string = ""
# Convert the given_num to string using str
strnum = str(given_num)
# calculating the length of string
length = len(strnum)
# Traverse the strnum string in reverse order using for loop range function
for index in range(length-1, -1, -1):
    # add the character to reverse_string using string concatenation
    reverse_string = reverse_string+strnum[index]
# converting the string to integer using int() function
reverse_number = int(reverse_string)
# print the result
print("The reversed number =", reverse_number)

Output:

The reversed number = 489

Method #3:Using Slicing

Approach:

  • Scan the given number.
  • Convert the given number to string using str() function.
  • Reverse the string using slicing
  • Convert this reversed string to integer to avoid leading zeros as mentioned in method #2.
  • Print the reversed string.

Below is the implementation:

# given number
given_num = 9840
# Convert the given_num to string using str
strnum = str(given_num)
# calculating the length of string
length = len(strnum)
# Reversing the string using slicing
reverse_string = strnum[len(strnum)::-1]
# converting the string to integer using int() function
reverse_number = int(reverse_string)
# print the result
print("The reversed number =", reverse_number)

Output:

The reversed number = 489

Method #4:Using list  and join functions

Approach:

  • Scan the given number.
  • Convert the given number to string using str() function.
  • Convert this string to list of digits using list() function.
  • Reverse the list using reverse() function
  • Join the list using join() function to get reversed string.
  • Convert this reversed string to integer to avoid leading zeros as mentioned in method #2.
  • Print the reversed string.

Below is the implementation:

# given number
given_num = 9840
# Convert the given_num to string using str
strnum = str(given_num)
# converting to list of digits
numberslist = list(strnum)
# reverse the list and
numberslist.reverse()
# convert this list to string using join
reverse_string = ''.join(numberslist)
# converting the string to integer using int() function
reverse_number = int(reverse_string)
# print the result
print("The reversed number =", reverse_number)

Output:

The reversed number = 489

Related Programs: