Given a integer as a input and replace all the ‘0’ with ‘5’ in the integer.
Examples:

    102 - 152
    1020 - 1525

Use of array to store all digits is not allowed.

Source: Amazon interview Experience | Set 136 (For SDE-T)

We strongly recommend to minimize the browser and try this yourself first.
The idea is simple, we get the last digit using mod operator ‘%’. If the digit is 0, we replace it with 5, otherwise keep it as it is. Then we recur for remaining digits.

[ad type=”banner”]

Following is C implementation of the above idea.

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20replace%20all%20%E2%80%980%E2%80%99%20with%20%E2%80%985%E2%80%99%20in%20an%20input%20Integer%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20recursive%20function%20to%20replace%20all%200s%20with%205s%20in%20an%20input%20number%0A%2F%2F%20It%20doesn’t%20work%20if%20input%20number%20itself%20is%200.%0Aint%20convert0To5Rec(int%20num)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%20for%20recursion%20termination%0A%20%20%20%20if%20(num%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Extraxt%20the%20last%20digit%20and%20change%20it%20if%20needed%0A%20%20%20%20int%20digit%20%3D%20num%20%25%2010%3B%0A%20%20%20%20if%20(digit%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20digit%20%3D%205%3B%0A%20%0A%20%20%20%20%2F%2F%20Convert%20remaining%20digits%20and%20append%20the%20last%20digit%0A%20%20%20%20return%20convert0To5Rec(num%2F10)%20*%2010%20%2B%20digit%3B%0A%7D%0A%20%0A%2F%2F%20It%20handles%200%20and%20calls%20convert0To5Rec()%20for%20other%20numbers%0Aint%20convert0To5(int%20num)%0A%7B%0A%20%20%20%20if%20(num%20%3D%3D%200)%0A%20%20%20%20%20%20%20return%205%3B%0A%20%20%20%20else%20return%20%20convert0To5Rec(num)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20num%20%3D%2010120%3B%0A%20%20%20%20printf(%22%25d%22%2C%20convert0To5(num))%3B%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”C Program” highlight=”” provider=”manual”/]

Output:

15125
[ad type=”banner”]