Define function to find the last even digit using recursion
Suppos开发者_运维知识库e input no is f(354683257) returns 2.
It sounds like you can break this into two easier problems.
- How do you find the last digit of a given number?
- How do you strip off the last digit of a given number?
Here's my solution. However, what are you supposed to do if there are no even digits in the number?
int findLastEvenDigit(int n)
{
lastDigit = n % 10;
if (lastDigit % 2 == 0) return lastDigit;
else return findLastEvenDigit(n/10);
}
Assumptions: No negative numbers (not sure/relevant if it will work for them)
精彩评论