Python Program to Interchange First and Last Digits of a Number

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

Be First to Comment

Leave a Reply

Your email address will not be published.