Reputation: 59
I need to write a Recursion function which takes in integer as input and returns the concatenation of the EVEN digits of the input number, i.e., we should remove the odd digits.
for example:
Creator(1234); return number: 24.
Creator(459876); return number: 486.
Well I'm pretty stuck in a dead end. I don't know how to return the right numbers.
That's my code I don't even know if I'm in the right way so.
My code:
int Creator(int n)
{
if (n == 0)
return;
if ((n % 10) % 2 != 0)
Creator(n / 10);
return n % 10;
}
Upvotes: 1
Views: 2402
Reputation: 40145
int Creator(int n){
if (n == 0)
return 0;//You must always return a value.
if (n % 2 != 0)
return Creator(n / 10);
return Creator(n / 10) * 10 + n % 10;//It is necessary to accumulate the call result.
}
Upvotes: 4