logo CodeStepByStep logo

You are working on problem set: Unit 4 ( Pause)

digitSum

Language/Type: Java recursion return
Author: Eric Roberts (on 2017/06/23)

Write a recursive method named digitSum that accepts an integer as a parameter and returns the sum of its digits. For example, calling digitSum(1729) should return 1 + 7 + 2 + 9, which is 19. If the number is negative, return the negation of the value. For example, calling digitSum(-1729) should return -19.

Constraints: Do not declare any global variables. Do not use any loops; you must use recursion. Do not use any auxiliary data structures like ArrayList, TreeMap, TreeSet, array, etc. Also do not solve this problem using a string. You can declare as many primitive variables like ints as you like. You are allowed to define other "helper" methods if you like; they are subject to these same constraints.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? .

If something seems wrong with our site, please

Is there a problem? Contact us.