,  

Replace all ‘0’ with ‘5’ in an input Integer

Problem: Given a integer as a input, replace all the ‘0’ with ‘5’ in the integer. Use of array to store all digits is not allowed.

For example:
Input:  n = 10030
Output: 15535

Input:  n = 10120
Output: 15125
The easy approach to this problem is to use mod operator %. We get the digit of the number using mod. If the digit is 0, we replace it with 5, otherwise keep it as it is. Following is the implementation of the idea.
#include <bits/stdc++.h>
using namespace std;

/* driver function */
int main()
{
    int n, new_n=0, lst;
    n = 10030;
    while(n)
    {
        /* get the last digit using mod operator */
        lst = n%10;

        if(lst == 0)
        {
            /* if last digit is 0, replace it with 5 */
            new_n = new_n*10 + 5;
        }
        else
        {
            /* else leave the digit as it is */
            new_n = new_n*10 + lst;
        }
        n/=10;
    }

    n=0;

    /* reversing the new_n to get the correct n */
    while(new_n)
    {
        n = n*10 + new_n%10;
        new_n/=10;
    }

    cout<< n <<"\n";

    return 0;
}
Output:
15535
Recursive approach:
#include<bits/stdc++.h>
using namespace std;

/* A recursive function to replace all 0s with
   5s in an input number */
int convNum(int num)
{
    /* Base case */
    if (num == 0)
        return 0;

    /* Extraxt the last digit and change it if needed */
    int lst = num % 10;
    if (lst == 0)
        lst = 5;

    /* Convert remaining digits and append the last digit */
    return convNum(num/10) * 10 + lst;
}

// Driver program to test above function
int main()
{
    int num = 10120;

    /* handling case when num is 0 itself */
    if(num==0)
        cout<<5<<"\n";
    else
        cout<<convNum(num)<<"\n";

    return 0;
}
Output:
15125