2022-06-13 08:49:36
def replaceDigit(x, d1, d2):
result = 0
multiply = 1
while (x // 10 > 0):
# Take remainder of number
# starting from the unit
# place digit
remainder = x % 10
# check whether it is
# equal to the digit
# to be replaced.if yes
# then replace
if (remainder == d1):
result = (result + d2 *
multiply)
else: # else remain as such
result = (result + remainder *
multiply)
# Update and move forward
# from unit place to hundred
# place and so on.
multiply *= 10
x = int(x / 10) # update the value
# check whether it is equal to the digit
# to be replaced.if yes then replace
if (x == d1):
result = result + d2 * multiply
else: # else remain as such
result = result + x * multiply
return result
# Driver code
x = 645
d1 = 6
d2 = 5
print(replaceDigit(x, d1, d2))
Python
Replace single digit code
Telegram - t.me/codingsolution_IT
8.8K views05:49