Friday, November 22, 2024
Google search engine
HomeLanguagesPython Program to Swap First and Last Digits of a Number

Python Program to Swap First and Last Digits of a Number

Python program to swap first and last digits of a given number; In this tutorial, you will learn how to swap or interchange first and last digits of a given number in python.

How to swap first and last digit of a number in python

  • Swap first and last digit of a number in python using while loop

Swap first and last digit of a number in python 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

  1. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  2. Python Program to Print ASCII Value of Character
  3. Write a Program to Calculate Simple Interest in Python
  4. Python Program to Compute Compound Interest
  5. Leap Year Program in Python
  6. Python Program to Print Star Pattern
  7. Number Pattern Programs in Python
  8. Python Program to Print Even and Odd numbers From 1 to N
  9. Python Abs() Function: For Absolute Value
  10. How to Check Whether a Number is Fibonacci or Not in Python
  11. Python: Program to Find Power of Number
  12. Python Program to Reverse a Numbers
  13. Python Program to Find Smallest/Minimum of n Numbers
  14. Python Program to Calculate Cube of Number
  15. Python Program to Find LCM of Two Numbers
  16. BMI (Body Mass Index) Calculator in Python
  17. Palindrome Program in Python using while loop, Function, etc
  18. Python: Program to Count Total Number of Bits in Number
  19. Python Random Number Generator Code
  20. Python Program to Calculate n-th term of a Fibonacci Series
  21. Zip Zap Zoom Python Program
  22. Python: program to convert Celsius to Fahrenheit
  23. Python Program to Swap Two Numbers
  24. Python Program to Get Standard Deviation
  25. Python Program to Find the Variance
  26. Python Program to Convert Height in cm to Feet and Inches
  27. Python Program to Convert Meters into Yards, Yards into Meters
  28. Python Program to Convert Kilometers to Meters, Miles
  29. Python Program to Find Perfect Number
  30. Python: Program to Find Strong Number
  31. Python Program Create Basic Calculator
  32. Python Program For math.floor() Method
  33. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
  34. Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
  35. Python Program to Find Roots of Quadratic Equation
  36. Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
  37. Python Program to Check Given Input is Alphabet, Number or Special Character
  38. Python Program to Check IF a Number is Power of Another Number
  39. Python Check Binary Representation of Given Number is Palindrome or Not
  40. Python Program to Find Area and Circumference of Circle using Radius
  41. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  42. Python Program to Print Binary Value of Numbers From 1 to N
RELATED ARTICLES

Most Popular

Recent Comments