Quick actions

cmd+k|ctrl+k

Navigation

Languages

Swap first and last digits

Snippet info

Language

Python

Visibility

public

Author

soham57

Created

2022-09-04T08:06:28.295936Z

Updated

2022-09-04T08:06:28.295936Z

import math

def firstLastDigit(num:int)->int:
    #Determining number of digits present in the input
    noOfDigits = int(math.log10(num))
    
    #Extracting lastdigit
    lastD=num%10
    
    #Extracting firstDigit
    firstD=int(num/ pow(10, noOfDigits))
    #print(num)
   # print(lastD, firstD)
    #Removed last digit and added the first didit to the last
    
    swapNum=int(num/10)*10+firstD
    
    #Adding the required part in the num
    required= (lastD-firstD)*(pow(10, noOfDigits))
    #Adding the required in the swapNum
    swapNum=required+swapNum
    
    
    return swapNum

print(firstLastDigit(7456542))
INFO