Practice Programs
Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.
Write a static recursive method that returns the number of digits in the integer passed to it as an argument of type
int. Allow for both positive and negative arguments. For example, −120 has three digits. Do not count leading zeros. Embed the method in a program, and test it.Write a static recursive method that returns the sum of the integers in the array of
intvalues passed to it as a single argument. You can assume that every indexed variable of the array has a value. Embed the method in a test program.One of the most common examples of recursion is an algorithm to calculate the
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access