How to print a number backwards in python
Example 1: Reverse a Number using a while loop
Output Show
4321 In this program, while loop is used to reverse a number as given in the following steps:
Example 2: Using String slicing
Output 654321 Using the string slicing concept, you can get reverse the string. It is the most asked programming question in the interview. We can reverse the integer number in Python using the different methods. Here we will write the program which takes input number and reversed the same. Let's understand the following methods of reversing the integer number.
Reverse a number using Python while loopFirst, we understand the algorithm of this program. It will make easy to understand the program logic. Once you get the logic, you can write the program in any language, not only Python. AlgorithmLet's implement the above algorithm in program. ProgramOutput: Enter the integer number: 12345 The reverse number is: 54321 Explanation - Let's understand this program step by step. We initialed a number variable for user input and variable revs_number initial value to null. First Iteration Reminder = number %10 Second Iteration Now the number is 123, and the revs_number is 5. The while checks its condition and executes for the next iteration. Reminder = Number % 10 Third Iteration From the Second Iteration, the values of both Number and Reverse have been changed as: number = 123 and revs_number = 54 Reminder = Number %10 Fourth Iteration The modified number is 12 and the revs_number is 543: Now while executes again. Reminder = Number %10 Fifth Iteration Reminder = Number %10 while loop is terminated because if found the false as a Boolean result. You can enter the different number and check the result. Reverse a Number Using RecursionLet's understand the following example. Output: Enter the number: 5426 The Reverse of entered number is = 6245 Logic is same in both programs. Once you understand the logic, it will easy to do it by own. How do you reverse a number backwards in Python?Program. # Ask for enter the number from the use.. number = int(input("Enter the integer number: ")). # Initiate value to null.. revs_number = 0.. # reverse the integer number using the while loop.. while (number > 0):. # Logic.. remainder = number % 10.. How do I reverse print a number?Let's see a simple c example to reverse a given number.. #include int main(). int n, reverse=0, rem;. printf("Enter a number: ");. scanf("%d", &n);. while(n!=0). How do you print a reverse number in Python?Reverse Number In Python. # Python Program to Reverse a Number using While loop.. Number = int(input("Please Enter any Number: ")). Reverse = 0.. while(Number > 0):. Reminder = Number %10.. Reverse = (Reverse *10) + Reminder.. Number = Number //10.. print("\n Reverse of entered number is = %d" %Reverse). Can we reverse a number in Python?Python makes it easy to reverse a number by using a while loop. We use a while loop with the help of both floor division and the modulus % operator.
|