Python program to swap first and last digits of a given number; Through this tutorial, i am going to show you python program for how to swap or interchange first and last digits of a given number.
Python Program to Swap First and Last Digits of a Number using While Loop
num =int(input('Please Enter Number: ')) rev = 0 noOfDigit = 0 temp = num while temp>0: temp = int(temp/10) noOfDigit = noOfDigit+1 if noOfDigit<2: print("\nIt is a Single-digit Number!") elif noOfDigit==2: temp = num while temp>0: rem = temp%10 rev = (rev*10)+rem temp = int(temp/10) print("\nFirst and Last (Second) Digit Interchanged Successfully!") print("\nNew Number: ") print(rev) else: temp = num while temp>0: rem = temp%10 rev = (rev*10)+rem temp = int(temp/10) revNum = rev rev = 0 temp = num noOfDigitTemp = noOfDigit while temp>0: remTemp = revNum%10 if noOfDigitTemp==noOfDigit: rem = temp%10 rev = (rev*10)+rem elif noOfDigitTemp==1: rem = temp%10 rev = (rev*10)+rem else: rev = (rev*10)+remTemp temp = int(temp/10) revNum = int(revNum/10) noOfDigitTemp = noOfDigitTemp-1 print('\n Interchanged Digits :- {}'.format(rev))
Output
Please Enter Number: 12325 Interchanged Digits :- 52321
Recommended Python Programs
Recommended:-How to Check Python Program Execution Time
Recommended:-Python Program Find Day of Week For Given Date
Recommended:-Python Program to Print Sum of All Array Elements
Recommended:-Python Program to Find the GCD of Two Numbers
Recommended:-Python Program to Find the LCM of the Array Elements
Recommended:-Python Code to Draw a Pie Chart
Recommended:-Python Program to Display the Current Date and Time
Recommended:-Python Program to Find Area of Triangle
Recommended:-Python Program to Find the Area of Rectangle
Recommended:-Python Program to Print All Timezones
Recommended:-Python Count the Occurrences in an Array
Recommended:-Python Program to Print Binary Numbers From 1 to N
Recommended:-Python Program to print Elements in a List
Recommended:-Python Program to Swap Characters of Given String
Recommended:-Swap First Two Characters in Each String in Python
Recommended:-Python Split String into Array of Characters
Recommended:-Python Program to Reverse String Using Stack
Recommended:-Python Insert Element at Specified Index in List
Recommended:-Python Program to Swap Two Elements in a List
Recommended:-How to Take Input List From User in Python
Recommended:-Python Program to Find LCM of Two Numbers