CPSC170A
Fundamentals of Computer Science II

Lab 4

Recursion

Multiples of 7

Details

Write the recursive function print_multiples(int max) that prints the multiples of 7 between 7 and max, in descending order. Assume that max is a multiple of 7.

Example

The function call:

print_multiples(98);

Would produce the output:

98
91
84
77
70
63
56
49
42
35
28
21
14
7


Powers of 2

Details

Write the recursive function print_powers(int max) that prints the powers of 2, in ascending order, that are less than or equal to max. Assume that max is a power of 2.

Example

The function call:

print_powers(64);

Would produce the output:

1
2
4
8
16
32
64

The function call:

print_powers(1024);

Would produce the output:

1
2
4
8
16
32
64
128
256
512
1024


Prompt

Details

Write the recursive function prompt(int min, int max) that repeatedly prompts the user to enter an integer until the user enters an integer in the range \([min, max]\).

Example

The function call:

prompt(0, 100);

Might produce the output:

Enter an integer in the range [0, 100]: -1
Enter an integer in the range [0, 100]: 101
Enter an integer in the range [0, 100]: 0

The function call:

prompt(0, 10);

Might produce the output:

Enter an integer in the range [0, 10]: -1
Enter an integer in the range [0, 10]: 11
Enter an integer in the range [0, 10]: 5