number = input("Enter a number:")
number = int(number)
reverse = 0
while number > 0:
rem = number % 10
reverse = reverse *10 + rem
number = number//10
print("Reverse:", reverse)
Learn Python with simple code, clear explanations, and examples
number = input("Enter a number:")
number = int(number)
reverse = 0
while number > 0:
rem = number % 10
reverse = reverse *10 + rem
number = number//10
print("Reverse:", reverse)
Enter a number:123 Reverse: 321 Enter a number:12321 Reverse: 12321
Examples of reversing a given number: Example #1 123 reverse of the number 321. Example #2 12321 reverse of the number 12321. Here i will explain the code with an example. number = input("Enter a number:") The input() function is used to get input from the user during runtime. For example: if the user input is 123, it will be assigned to the variable 'number' as str(string) object. number = "123" number = int(number) The number "123" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'number'. number = 123 reverse = 0 The variable 'reverse' is initialised to zero. while number > 0: rem = number % 10 reverse = reverse *10 + rem number = number//10 #First loop While number > 0: => while 123 > 0: which is True, therefore, the body of the loop will be executed: rem = 123 % 10 => rem = 3 By doing the above operation, the rem variable is assigned with the last digit of the number 123, ie. number 3 reverse = reverse*10 + rem => reverse = 0*10 + 3 => reverse = 0 + 3 => reverse = 3 By doing the above operation, the variable is stored with the value of 'reverse multiplied by 10 + remainder'. Currently the value of the variable 'reverse' is 0. So, 0 multiplied by 10 + 3 is assigned to the variable 'reverse', ie. 3. number = number//10 number = 123 // 10 number = 12 Before going to the next loop, we need to have only the first two digits of number 123 because the operation on last digit is already performed in the current loop(first loop). The floor division of number 123 by 10 is 12 (integer value is only assigned back to variable 'number'). By the end of the first loop, the current values of variables are as shown below. reverse = 1 number = 12 #Second loop While number > 0: => while 12 > 0: which is True, therefore, the body of the loop will be executed: rem = 12 % 10 => rem = 2 By doing the above operation, the rem variable is assigned with the second digit of the three digit number 123, ie. number 2 reverse = reverse*10 + rem => reverse = 3*10 + 2 => reverse = 30 + 2 => reverse = 32 By doing the above operation, the variable is stored with the value of 'reverse multiplied by 10 + remainder'. Currently the value of the variable 'reverse' is 3. So, 3 multiplied by 10 + 2 is assigned to the variable 'reverse', ie. 32. number = number//10 number = 12 // 10 number = 1 Before going to the next loop, we need to have only the first digit of number 12 because the operation on digit 2 is already performed in the current loop(second loop). The floor division of number 12 by 10 is 1 (integer value is only assigned back to variable 'number'). By the end of the second loop, the current values of variables are as shown below. reverse = 32 number = 1 #Third loop While number > 0: => while 1 > 0: which is True, therefore, the body of the loop will be executed: rem = 1 % 10 => rem = 1 By doing the above operation, the rem variable is assigned with the first digit of the three digit number 123, ie. number 1 reverse = reverse*10 + rem => reverse = 32*10 + 1 => reverse = 320 + 1 => reverse = 321 By doing the above operation, the variable is stored with the value of 'reverse multiplied by 10 + remainder'. Currently the value of the variable 'reverse' is 12. So, 32 multiplied by 10 + 1 is assigned to the variable 'reverse', ie. 321. number = number//10 number = 1 // 10 number = 0 Before going to the next loop, we need to have only the remaining digits of number 1 because the operation on digit 1 is already performed in the current loop(third loop). The floor division of number 1 by 10 is 0 (integer value is only assigned back to variable 'number'). But the value of variable 'number' is now zero, which means operations are performed on all the digits of the number 123 and no more operations are left unperformed. By the end of the third loop, the current values of variables are as shown below. reverse = 321 number = 0 #Fourth loop [not executed] While number > 0: => while 0 > 0: which is False, therefore, the body of the loop will not be executed and control of the loop comes outside the loop. print("Reverse:", reverse) Now the statement 'print("Reverse:", reverse)' will get executed and the output will be 'Reverse: 321'.