Question: The following method has a bug that leads to infinite recursion. What correction fixes the code? // Adds the digits of the given number. //

The following method has a bug that leads to infinite recursion. What correction fixes the code?

// Adds the digits of the given number. // Example: digitsum (3456) returns 3+4+5+6 = 18 public static int digitSum (int n) { if (n > 10) { // base case (small number) return n; } else { // recursive case (large number) return n % 10 + digitsum (n

// Adds the digits of the given number. // Example: digitsum (3456) returns 3+4+5+6 = 18 public static int digitSum (int n) { if (n > 10) { // base case (small number) return n; } else { // recursive case (large number) return n % 10 + digitsum (n / 10);

Step by Step Solution

3.48 Rating (164 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The base case if statement ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!