{"id":6920,"date":"2021-05-24T09:31:00","date_gmt":"2021-05-24T04:01:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=6920"},"modified":"2021-11-22T18:39:26","modified_gmt":"2021-11-22T13:09:26","slug":"write-a-program-to-reverse-a-number-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/write-a-program-to-reverse-a-number-in-python\/","title":{"rendered":"Write a Program to Reverse a Number in Python | Reverse Digits or Integers"},"content":{"rendered":"
Given a number, thee task is to reverse the given number in Python.<\/p>\n
Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n There are several ways to reverse the given number in python some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Algorithm:<\/strong><\/p>\n Below is the implemenatation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:\u00a0<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n 1)For numbers without trailing zeroes<\/strong><\/p>\n Output:<\/strong><\/p>\n Note: <\/strong><\/p>\n Here it gives the correct result as their are no trailing zeroes<\/p>\n Let us consider a case where the given number contains trailing zeroes .<\/p>\n EX: 9840<\/strong><\/p>\n The above algorithm gives the output<\/p>\n Here it also prints the leading zeroes so to avoid this the solution is given below.<\/p>\n Solution:<\/strong><\/p>\n After getting the reversed string convert the string to integer using int()<\/strong> function which removes the leading zeroes<\/p>\n as below.<\/p>\n 2)For numbers with trailing zeroes<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nnumber=12345<\/pre>\n
The reversed number = 54321<\/pre>\n
After reversing the number we get 54321<\/pre>\n
number=7341<\/pre>\n
The reversed number = 1437<\/pre>\n
number=9840<\/pre>\n
The reversed number = 489<\/pre>\n
Here the reversed number is 0489 we neglect the leading zero so the reversed number is 489<\/pre>\n
Reverse the given Number in Python<\/h2>\n
\n
Method #1:Using while loop<\/h3>\n
\n
# given number\r\ngiven_num = 12345\r\n\r\n# Take a variable reverse_number and initialize it to null\r\nreverse_number = 0\r\n\r\n# using while loop to reverse the given number\r\n\r\nwhile (given_num > 0):\r\n # implementing the algorithm\r\n # getting the last digit\r\n remainder = given_num % 10\r\n reverse_number = (reverse_number * 10) + remainder\r\n given_num = given_num \/\/ 10\r\n\r\n# Display the result\r\nprint(\"The reversed number =\", reverse_number)\r\n<\/pre>\n
The reversed number = 54321<\/pre>\n
Method #2: Using for loop and string concatenation<\/h3>\n
\n
# given number\r\ngiven_num = 12345\r\n# taking empty string\r\nreverse_string = \"\"\r\n# Convert the given_num to string using str\r\nstrnum = str(given_num)\r\n# calculating the length of string\r\nlength = len(strnum)\r\n# Traverse the strnum string in reverse order using for loop range function\r\nfor index in range(length-1, -1, -1):\r\n # add the character to reverse_string using string concatenation\r\n reverse_string = reverse_string+strnum[index]\r\n# print the result\r\nprint(\"The reversed number =\", reverse_string)\r\n<\/pre>\n
The reversed number = 54321<\/pre>\n
The reversed number = 0489<\/pre>\n
# given number\r\ngiven_num = 9840\r\n# taking empty string\r\nreverse_string = \"\"\r\n# Convert the given_num to string using str\r\nstrnum = str(given_num)\r\n# calculating the length of string\r\nlength = len(strnum)\r\n# Traverse the strnum string in reverse order using for loop range function\r\nfor index in range(length-1, -1, -1):\r\n # add the character to reverse_string using string concatenation\r\n reverse_string = reverse_string+strnum[index]\r\n# converting the string to integer using int() function\r\nreverse_number = int(reverse_string)\r\n# print the result\r\nprint(\"The reversed number =\", reverse_number)\r\n<\/pre>\n
The reversed number = 489<\/pre>\n
Method #3:Using Slicing<\/h3>\n
\n
# given number\r\ngiven_num = 9840\r\n# Convert the given_num to string using str\r\nstrnum = str(given_num)\r\n# calculating the length of string\r\nlength = len(strnum)\r\n# Reversing the string using slicing\r\nreverse_string = strnum[len(strnum)::-1]\r\n# converting the string to integer using int() function\r\nreverse_number = int(reverse_string)\r\n# print the result\r\nprint(\"The reversed number =\", reverse_number)\r\n<\/pre>\n
The reversed number = 489<\/pre>\n
Method #4:Using list\u00a0 and join functions<\/h3>\n
\n
# given number\r\ngiven_num = 9840\r\n# Convert the given_num to string using str\r\nstrnum = str(given_num)\r\n# converting to list of digits\r\nnumberslist = list(strnum)\r\n# reverse the list and\r\nnumberslist.reverse()\r\n# convert this list to string using join\r\nreverse_string = ''.join(numberslist)\r\n# converting the string to integer using int() function\r\nreverse_number = int(reverse_string)\r\n# print the result\r\nprint(\"The reversed number =\", reverse_number)\r\n<\/pre>\n
The reversed number = 489<\/pre>\n
\n