Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

largestDigit

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.