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

PROBLEM :

Given a number your task is to complete the function convertFive which takes an integer n as argument and replaces all zeros in the number n with 5 .Your function should return the converted number .

Input:
The first line of input contains an integer T denoting the number of test cases . Then T test cases follow . Each test case contains a single integer n denoting the number.

Output:
The output of the function will be an integer where all zero's are converted to 5 .

Constraints:
1<=T<100
1<=n<=10000

Example:
Input
2
1004
121

Ouput
1554
121

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*you are required to complete this method*/

int convertFive(int n)
{
    int k,r ;
    k=0 ;
   
    while(n!=0)
    {
        r=n%10 ;
        if(r!=0)
            k=k*10+r ;
        else
            k=k*10+5 ;
        n=n/10 ;
    }
   
    while(k!=0)
    {
        r=k%10 ;
        n=n*10+r ;
        k=k/10 ;
    }
    return n ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )