Homework 24: Recursion
Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.
(1 point) Write a iterative method int sumOfDigits(int x)
that returns the sum of the digits of an integer.
If x is 1234, the function should return 1 + 2 + 3 + 4, that is, 10.
If x is 345, the function should return 3 + 4 + 5 = 12.
If x is 3, the function should return 3.
If x is negative, ignore the minus sign. For example, -12 and 12 both return 3.
(Hint: Use / 10
and % 10
.)
(1 point) Now, implement int sumOfDigits(int x)
using recursion – no explicit loop constructs allowed!