Previous Post :
Python #18-Counting the number of digits in the given number
Next Post :
Python #20-Given number is Prime or Not
Reversing of the given number
Eg: 54321
The reversing of the given number will be 12345
Source Code:
number = int (input("Enter the number to reverse: "))
rev = 0
while(number > 0):
r = number % 10
rev = (rev * 10)+r
number = number / 10
print "Reversed of the given number is :", rev
Output:
Enter the number to reverse: 54321
Reversed of the given number is : 12345
Explanation:
Python #18-Counting the number of digits in the given number
Next Post :
Python #20-Given number is Prime or Not
Reversing of the given number
Eg: 54321
The reversing of the given number will be 12345
Source Code:
number = int (input("Enter the number to reverse: "))
rev = 0
while(number > 0):
r = number % 10
rev = (rev * 10)+r
number = number / 10
print "Reversed of the given number is :", rev
Output:
Enter the number to reverse: 54321
Reversed of the given number is : 12345
Explanation:
1. Getting the number to be reversed from the user by using input().
2. Initializing a variable rev=0.
3. Checking whether the given number is greater than Zero. If the condition is said to be true the while loops starts its function.
4. First creating a temp variable r and upcoming operation modules (i.e r = number %10). This will give you the reminder of the number which will be stored in r.
5. Next rev = (rev * 10)+r performing the operation will give you a number which is stored of in rev.
6. Now, then by removing of the last digits in a number operation will be performed by this statement (i.e) number = number / 10 is used to remove the last digits of the number.
7. Like wise the Loop will run until the condition fails.
8. At Last print the rev where the reverse number will be stored.
9. End
Thanks and regards,
Tech bird
Comments