Chapter 23

Answers to Chapter 23 Review Questions

1: What is the output from the following method if you call it with the argument 1?
01: public static void Recurs(int number)
02: {
03:     if(number > 8)
04:         return;
05:     else
06:     {
07:         Console.WriteLine("Number: { 0} ", number);
08:         Recurs(number * 2);
09:     }
10: }
A1:

Number: 1

Number: 2

Number: 4

Number: 8

2: Identify in the Recurs method of question 1 the main ingredients found in most successful recursive methods.
A2: A branching statement (if-else) spanning lines 3–9. A base case in line 4, a recursive call (or recursive step) in line 8. Each recursive call moves towards the base case.
3: Rewrite the Recurs method in question 1 so that it (by still using recursion) provides the following output if ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.