logo Practice-It logo


Language/Type: Java recursion recursive programming
Author: Marty Stepp (on 2011/02/16)

Write a recursive method largestDigit that accepts an integer parameter and returns the largest digit value that appears in that integer. Your method should work for both positive and negative numbers. If a number contains only a single digit, that digit's value is by definition the largest. The following table shows several example calls:

Call Value Returned
largestDigit(14263203) 6
largestDigit(845) 8
largestDigit(52649) 9
largestDigit(3) 3
largestDigit(0) 0
largestDigit(-573026) 7
largestDigit(-2) 2

Obey the following restrictions in your solution:

  • You may not use a String, Scanner, array, or any data structure (list, stack, map, etc.).
  • Your method must be recursive and not use any loops (for, while, etc.).
  • Your solution should run in no worse than O(N) time, where N is the number of digits in the number.
Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.