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
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variablenumwith the statements/strings("Enter the integer: "), we use theintfunction and declare the input value as an integer value. - We assign the variable
reversed_numwith the integer0. - Next, we declare a
whileloop with the conditionnum != 0followed by a:, after which we declare the conditiondigit = num % 10andreversed_num = reversed_num * 10 + digitwherenum //= 10. - In this code, the while loop is iterated until the test expression
num != 0is equal to0. During each iteration, the remainder of thenumdivided by 10 is stored in the variabledigit. During each iterationdigitreduces by one and when thewhileloop exits , the reversed value is already in the variablereversed_num. - In the STDIN section of the code editor the input values are entered.
- The final output values are displayed with the statement
("\nReversed Number: " + str(reversed_num))using theprintfunction, where the+ str(reversed_num)returns and displays the string version of thereversed_numvalue.
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
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variablenumwith the statements/strings("Enter the integer: "), we use theintfunction and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- The
printfunction will display the statement("\nReversed Number:",(str(num)[::-1])), where the function(str(num)[::-1])corresponds tostart:stop:step - 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 variablenumholds.
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.