Python Program to Reverse a Number

In this python tutorial, you will learn how to Reverse a Number using the if and else statements, while loop along with the different operators of the python programming language.

How to Reverse a Number?

Let’s take a look at the first source code , here the values are given as input by the user in the code, the operators and while loop along with the assignment operators carry out the function.

RUN CODE SNIPPET
#Python Program to Reverse a Number using while loop

num =int(input("Enter the integer: ")) 
reversed_num = 0

while num != 0:
    digit = num % 10
    reversed_num = reversed_num * 10 + digit
    num //= 10

print("\nReversed Number: " + str(reversed_num))

INPUT:

1234

OUTPUT:

Enter the integer: 
Reversed Number: 4321
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num with the statements/strings ("Enter the integer: ")  , we use the int  function and declare the  input value as an integer value.
  2. We assign the variable reversed_num with the integer 0.
  3. Next, we declare a while loop with the condition num != 0 followed by a :, after which we declare the condition digit = num % 10 and  reversed_num = reversed_num * 10 + digit where num //= 10.
  4. In this code, the while loop is iterated until the test expression num != 0 is equal to 0. During each iteration, the remainder of the  num divided by 10 is stored in the variable digit. During each iteration digit reduces by one and when the while loop exits , the reversed value is already in the variable reversed_num.
  5. In the STDIN section of the code editor the input values are entered.
  6. The final output values are displayed with the statement ("\nReversed Number: " + str(reversed_num)) using the print function, where the + str(reversed_num) returns and displays the string version of the reversed_num value.

Let’s take a look at the second source code , here the values are given as input by the user in the code, inbuilt methods and print statement carry out the function.

RUN CODE SNIPPET
#Python Program to Reverse a Number using string slicing

num =int(input("Enter the integer: "))
print("\nReversed Number:",(str(num)[::-1])) 

INPUT:

123456

OUTPUT:

Enter the integer: 
Reversed Number: 654321
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num with the statements/strings ("Enter the integer: ")  , we use the int  function and declare the  input value as an integer value.
  2. In the STDIN section of the code editor the input values are entered.
  3. The print function will display the statement ("\nReversed Number:",(str(num)[::-1])) , where the function (str(num)[::-1]) corresponds to start:stop:step
  4. When you use [::-1] it starts from the end towards the first taking each element, so it reverses and will display the length of the value in string format which the variable num holds.

NOTE:

  • The != operator is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal.
  • The % modulus operator functions returns modulus of the given arguments.
  • The * multiplication operator returns product of the given arguments.
  • The //= operator know as the double division assignment operator lets you divide two values together and assign the resultant value to a variable.
  • The While true loop in python runs without any conditions until the break statement executes inside the loop.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The str() function in python returns the string version of the object.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.
  • The input() function allows a user to insert a value into a program, it returns a integer value.
  • The statement for the input function are enclosed in single quotes and parenthesis.