开发者

Define function to find the last even digit using recursion

开发者 https://www.devze.com 2023-02-26 12:47 出处:网络
Suppos开发者_运维知识库e input no is f(354683257) returns 2.It sounds like you can break this into two easier problems.

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消