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

Given an integer N, the task is to replace all the 0's that appear in the number with '5'. However, leading zeros are ignored as they don't affect the number's value. For example, if N = 1007, the output should be 1557.

Example Cases

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 = 105

Output

155

Explanation − The zero in the middle is replaced with '5', resulting in 155.

Using String Replacement Method

The simplest approach is to convert the number to a string and replace all '0' characters with '5' ?

def replace_zeros_with_fives(number):
    # Convert number to string and replace '0' with '5'
    result_str = str(number).replace('0', '5')
    return int(result_str)

# Test the function
number = 1007
result = replace_zeros_with_fives(number)
print(f"Original number: {number}")
print(f"After replacing 0s with 5s: {result}")

# Test with another example
number2 = 10203
result2 = replace_zeros_with_fives(number2)
print(f"Original number: {number2}")
print(f"After replacing 0s with 5s: {result2}")
Original number: 1007
After replacing 0s with 5s: 1557
Original number: 10203
After replacing 0s with 5s: 15253

Using Mathematical Approach

We can solve this mathematically by extracting digits and reconstructing the number ?

def replace_zeros_mathematical(number):
    if number == 0:
        return 5
    
    result = 0
    place_value = 1
    
    while number > 0:
        digit = number % 10
        if digit == 0:
            digit = 5
        result = digit * place_value + result
        place_value *= 10
        number //= 10
    
    return result

# Test the function
test_numbers = [1007, 10203, 50040, 0]

for num in test_numbers:
    result = replace_zeros_mathematical(num)
    print(f"Original: {num}, Modified: {result}")
Original: 1007, Modified: 1557
Original: 10203, Modified: 15253
Original: 50040, Modified: 55545
Original: 0, Modified: 5

Using Recursion

Here's a recursive approach that processes digits from right to left ?

def replace_zeros_recursive(number):
    # Base case: if number is 0, return 5
    if number == 0:
        return 5
    
    # Base case: if number is single digit
    if number < 10:
        return 5 if number == 0 else number
    
    # Get the last digit
    last_digit = number % 10
    remaining_number = number // 10
    
    # Replace 0 with 5 in last digit
    if last_digit == 0:
        last_digit = 5
    
    # Recursively process the remaining number
    return replace_zeros_recursive(remaining_number) * 10 + last_digit

# Test the recursive function
test_cases = [1007, 10203, 304050, 1000]

for num in test_cases:
    result = replace_zeros_recursive(num)
    print(f"Original: {num}, Modified: {result}")
Original: 1007, Modified: 1557
Original: 10203, Modified: 15253
Original: 304050, Modified: 354555
Original: 1000, Modified: 1555

Comparison

Method Time Complexity Space Complexity Best For
String Replacement O(n) O(n) Simple and readable
Mathematical O(n) O(1) Memory efficient
Recursive O(n) O(n) Educational purposes

Conclusion

The string replacement method is the most straightforward and readable approach. Use the mathematical method when memory efficiency is important, and the recursive approach is good for understanding the digit-by-digit processing concept.

Updated on: 2026-03-25T15:46:50+05:30

785 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements