# Write a program in Python to replace all the 0’s with 5 in a given number

PythonServer Side ProgrammingProgramming

Given an Integer N the task is to replace all the 0’s that appear in the number with ‘5’. However, the number with leading ‘0’ cannot be replaced with ‘5’ as it remains unchanged. For example,

Input-1

N = 1007

Output

1557

Explanation − The given number has 2 zeros which when replaced by ‘5’ results in the output as 1557.

Input-2

N = 00105

Output

155

Explanation − Since the given number starts with the leading ‘0’ which can be ignored and the output after replacing the 0 in the middle with ‘5’ results the output as 155.

## Approach to solve this problem

To replace all the 0’s in the given number with ‘5’ we can find and extract the last digit of the number. If the last digit of that number is ‘0’ then change and replace the value with ‘5’ and extract another digit. However, any leading ‘0’s in the given number must be ignored. Thus we will first extract the last digit and then again call the same function while extracting the other digit of that number.

• Take Input a number N.

• An Integer function convertToFive(int N) takes a number as input and returns the modified number by replacing all 0’s with ‘5’.

• If the last digit of the number is ‘0’ then replace the value with ‘5’.

• Return the recursive function which takes another digit of the number by dividing ‘10’ and multiplying by ‘10’.

• Return the output which extracts the last digit by adding into it.

## Example

def convertToFive(number):
return number
def calculateValue(number):
result = 0
placeValu = 1
if (number == 0):
result += (5 * placeValue)
while (number > 0):
if (number % 10 == 0):
result += (5 * placeValue)
number //= 10
placeValue *= 10
return result
print(covertToFive(14006))

## Output

Running the above code will generate the output as,

14556

Since there are two 0 in the given number, after replacing the number 14006, it will become 14556.