Lab
Recursion Exercises

Write a menu-driven program that completes each task using a recursive function. A shell program is available from your teacher.

• Write a function called `letters` that takes a lower case letter as its argument and displays the sequence of letters from 'a' to the given letter.
For example:
`Letters('a');` displays `a` and `Letters('d');` displays `abcd`

• Write a function called `twos` that takes a single integer as its argument and returns the number of factors of 2 in the number. (Hint: odd numbers have no factors of 2, numbers that are twice an odd number have one, numbers that are four times an odd have two, and so on.)
For example:
` System.out.print(twos(-12)); returns 2`

• Write a Boolean function called `powerof3` that takes a single positive integer argument and returns true iff the integer is a perfect power of 3 such as 1, 3, 9, 27, 81, ...
For example:
```if (powerof3(81))
System.out.println("81 is a power of 3.");
else
System.out.println("81 is not a power of 3.";```
displays `81 is a power of 3`

• Write a function called `reverse` that takes a single long integer argument and returns the result of reversing its digits. For partial credit, write a void function that just displays the number in reverse.
For example:
```System.out.print(reverse(-12)); returns -21 System.out.print(reverse(1234567)); returns 7654321```

• Write a function called `base5` that takes a single nonnegative integer argument and displays its base five equivalent.
For example:
`base5(136);` displays ```1021 base5(5);``` displays `10`

• Bonus! Write a function called `printWithCommas` that takes a single nonnegative long integer argument and displays it with commas inserted properly.
For example:
`printWithCommas(12045670);` displays ```12,045,670 printWithCommas(1);``` displays `1`

Continue to:  Unit 3 / Prev / Next