Replace all ‘0’ with ‘5’ in an input Integer (Function Problem)
PROBLEM :
Given a number your task is to complete the function convertFive which replace all zeros in the number with 5 and returns the 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 :
--------------------------------------------------------------------------------
/*
Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/
/*you are required to complete this method*/
int convertFive(int n)
{
if(!n) return 0 ;
int curr=n%10 ;
n/=10 ;
if(!curr) curr=5 ;
return convertFive(n)*10+curr ;
}
---------------------------------------------------------------------------------
Given a number your task is to complete the function convertFive which replace all zeros in the number with 5 and returns the 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 :
--------------------------------------------------------------------------------
/*
Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/
/*you are required to complete this method*/
int convertFive(int n)
{
if(!n) return 0 ;
int curr=n%10 ;
n/=10 ;
if(!curr) curr=5 ;
return convertFive(n)*10+curr ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment