This question already has an answer here:
I think I have seen a nice riddle that you may like.
(This riddle has been translated so I hope that no valuable information has been lost):
Two friends walk in distant lands when they are trapped by the men of an evil king. They are imprisoned in different rooms without having time to talk to each other.
Both have a window in their room from which they see a garden.
One of them (Rick) can see 16 apple trees.
The other (James) can see 8 apple trees.
It is indicated that in the garden there are 24 or 26 apple trees, that none of the apple trees has been seen by both at the same time, and that between the two they can see all the apple trees in the garden.
During the following hours, the king will ask the same question. First, ask Rick: Are there 24 or 26 apple trees in the garden? "If he does not answer, he will ask James the same question.
If one of the two friends responds correctly, both will be released.
If one of the two responds incorrectly, both will be executed.
If they do not answer, the king will ask the other friend, a round consists of: "Ask Rick - ask James", always starting with Rick.
How many rounds are necessary to answer correctly?